Java为mysql

时间:2016-07-20 18:34:49

标签: java mysql sql

我可能知道在java中插入sql语句有什么问题,因为sql语句允许我将所有内容插入数据库,让我的profilepic为空。目前,我试图做的是,我试图将userId = 143中的图像复制到新用户。但是,图像的重复在java中不起作用。

另一方面,在mysql数据库注册表中,有userId = 143和image的数据。我尝试执行以下sql语句,它完全符合我的要求。

    insert into registration 
(profilepic, firstName,lastName,phoneNo,dateOfBirth,displayname,password,
emailAddress, address, interest ) 
select (select profilepic from registration where userId = 
143),'ab','cd',94329,'2016-11-11','af','de','gf','ge','ee' ;

但是,如果我在java上使用sql语句的确切格式,它就不起作用。我无法弄清楚我的sql语句有什么问题。非常感谢您的帮助。感谢。

 try{
                                    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                                conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
                                    // connects to the database
                                    conn = getConnection();

                                    // constructs SQL statement
                                  stmt = conn.createStatement();


                                    String sql1 =" insert into registration (profilepic, firstName, lastName,phoneNo,dateOfBirth,displayname,password,emailAddress, address, interest ) select (select profilepic from registration where userId = 143),?,?,?,?,?,?,?,?,? ";
                                    //Using a PreparedStatement to save the file
                                    PreparedStatement statement1 = conn.prepareStatement(sql1);
                                    statement1.setString(1, firstName);
                                    statement1.setString(2, lastName);
                                    statement1.setString(3, phoneNo);
                                    statement1.setString(4, dateOfBirth); 
                                  statement1.setString(5, displayName);
                                    statement1.setString(6, password);

                                    statement1.setString(7,emailAddress);
                                    statement1.setString(8, address);


                                    statement1.setString(9, interest);



                                    //sends the statement to the database server
                                    int row = statement1.executeUpdate();
                                    if (row > 0) {
                                      getServletContext().getRequestDispatcher("/Message.jsp").include(request, response);
                                      //  message = "You have successfully registered.";
                                    }


                                } catch (SQLException ex) {
                                //  message = "You have failed to registered. Please try again.";
                                 //   ex.printStackTrace();
                                } finally {
                                    if (conn != null) {
                                        // closes the database connection
                                        try {
                                            conn.close();
                                        } catch (SQLException ex) {
                                            // ex.printStackTrace();
                                            //silent
                                            //message="You have failed to log in";
                                             getServletContext().getRequestDispatcher("/FailedMsg.jsp").include(request, response);
                                        }
                                    }




                          }  

1 个答案:

答案 0 :(得分:0)

尝试修改您的查询,如下所示只有一个SELECT语句并避免内部子选择。

insert into registration 
(profilepic, firstName,lastName,phoneNo,dateOfBirth,displayname,password,
emailAddress, address, interest ) 
select profilepic,'ab','cd',94329,
'2016-11-11','af','de','gf','ge','ee'
from registration 
where userId = 143;