java - 数据无法正常存入mysql

标签 java mysql jsp

我正在尝试在 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/

相关文章:

mysql - 如何为具有组合主键的表创建索引?

当存在空字段时MySQL串联

java - 使用 JSP 编写一个小部件生成器以在其他 servlet 中使用

java - 用于确定图像属性的 Android Native API(getWidth 等)返回错误值

mysql - 从日期时间戳中选择用户 - 仅返回上周

javascript - 使用strutst html标签时,如何在提交按钮上调用onclick事件?

java - Spring boot 添加多行

java - JSP 超文本链接到索引页

java - 安卓/ Gradle : how to find the good mix of Google dependencies versions?

java - 文件的访问权限