有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java数据无法正确保存到mysql中

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


共 (1) 个答案

  1. # 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();
    

    为什么要准备声明: