【问题标题】:data can not save into mysql properly数据无法正确保存到mysql
【发布时间】:2015-12-05 07:01:02
【问题描述】:

我正在尝试在 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 并且日期和时间正确保存。帮帮我。谢谢你提前。

【问题讨论】:

  • uname1 中没有数据?
  • 了解准备好的声明。
  • 首先检查uname1是否有任何数据,是的,您的id必须是AI才能自动插入。
  • 尝试使用 sysout 显示 uname1pwd 值,以验证它们的值是否不同于 null
  • 为了连接池和 sql-injection,改变你连接和执行 DML 的方式。你确定你最后没有任何触发器?也许后端 sql 拒绝插入或事务性问题。

标签: java mysql jsp


【解决方案1】:

您的陈述中有错字。我猜你的意思是桌子logout

"insert into logut values ('" + 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();

为什么要准备声明:

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2021-10-26
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多