我正在尝试在 jsp netbeans 中创建登录时间和注销时间 Web 应用程序。当我尝试将注销时间保存到 mysql 数据库中时,日期和时间保存正确,但用户名和密码都保存为空。请帮助我将用户名和密码正确保存到表中。 这是我的注销代码:
`<%@ page import ="java.sql.*" %>
`<%String url="jdbc:mysql://localhost:3306/mysql";`
` String user="root";`
` String password="";`
`Class.forName("com.mysql.jdbc.Driver");`
`Connection con = DriverManager.getConnection(url, user, password);`
`Statement st = con.createStatement();`
`String uname1= request.getParameter("first_name");`
`String pwd = request.getParameter("pass");`
`session.setAttribute("fname", uname1);`
`session.setAttribute("pass", pwd);`
`int i = st.executeUpdate("insert into logut values ('" + uname1 + "','" + pwd + "',now())");`
`if (i > 0) `
`{out.write("<script type='text/javascript'>\n");`
`out.write("alert(' Logout Successfully!!! ');\n");`
`out.write("setTimeout(function({window.location.href='index.jsp'},1000);");`
`out.write("</script>\n");`
`}`
%>`
我的数据库是这样保存的:id=null pass=null 并且日期和时间正确保存。帮帮我。谢谢你提前。
最佳答案
您的陈述有错别字。我猜你的意思是表 logout
“插入注销值 ('” + uname1 + “','” + pwd + “',now())”
但除此之外,您确实必须考虑准备好的语句。
String insertTableSQL = "INSERT INTO DBUSER"
+ "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
+ "(?,?,?,?)";
PreparedStatement preparedStatement = dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setInt(1, 11);
preparedStatement.setString(2, "username");
preparedStatement.setString(3, "password");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
// execute insert SQL statement
preparedStatement .executeUpdate();
为什么准备语句:
关于java - 数据无法正常存入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32477344/