java数组元素没有插入mysql表,尽管没有显示错误

flvtvl50  于 2021-06-19  发布在  Mysql
关注(0)|答案(3)|浏览(430)

这是我正在进行的sql查询:

stmt = conn.createStatement();
        String sql="INSERT INTO registration(phone,name,address,city,destination,date) VALUE ("+phone[i]+",'"+name[i]+"','"+address[i]+"','"+city[i]+"','"+destination[i]+"','"+date[i]+"')";

这是程序的相关部分,我在这里对上下文进行sql查询。这个问题似乎是正确的。程序没有抛出任何错误,但是数据没有被插入mysql表

case 1: {
                        System.out.println("Enter your Phone number:");
                        phone[i] = sc.nextInt();
                        sc.nextLine();

                        System.out.println("Enter your name:");
                        name[i] = sc.nextLine();

                        System.out.println("Enter your address:");
                        address[i] = sc.nextLine();

                        System.out.println("Enter your Pick up city:");
                        city[i] = sc.nextLine();

                        System.out.println("Enter your Destination:");
                        destination[i] = sc.nextLine();

                        System.out.println("Enter your Date:");
                        date[i] = sc.nextLine();
                        ++i;
                        Connection conn = null;
                        Statement stmt = null;
                        try{
                            //STEP 2: Register JDBC driver
                            Class.forName("com.mysql.jdbc.Driver");

                            //STEP 3: Open a connection
                            System.out.println("Connecting to a selected database...");
                            conn = DriverManager.getConnection(DB_URL, USER, PASS);
                            System.out.println("Connected database successfully...");

                            //STEP 4: Execute a query
                            System.out.println("Inserting records into the table...");
                            stmt = conn.createStatement();
                           String sql="INSERT INTO registration(phone,name,address,city,destination,date) VALUE ("+phone[i]+",'"+name[i]+"','"+address[i]+"','"+city[i]+"','"+destination[i]+"','"+date[i]+"')";

                        }catch(SQLException se){
                            //Handle errors for JDBC
                            se.printStackTrace();
                        }catch(Exception e){
                            //Handle errors for Class.forName
                            e.printStackTrace();
                        }finally{
                            //finally block used to close resources
                           try{
                                if(stmt!=null)
                                    conn.close();
                            }catch(SQLException se){
                            }// do nothing
                            try{
                                if(conn!=null)
                                    conn.close();
                            }catch(SQLException se){
                                se.printStackTrace();
                            }//end finally try
                        }//end try
                        System.out.println("Goodbye!");
                        break;
                    }

程序没有抛出任何错误,但mysql显示没有插入新条目。为什么会这样?

k2arahey

k2arahey1#

您只是在这里创建语句,那里的查询将不会运行。我已经添加了一个代码行运行sql查询后,下面的行,请看下面的代码。我还看到您编写的sql查询有一个小错误。请把它正确(我做了下面的代码)。否则,它将在运行代码后引发异常。

case 1: {
                        System.out.println("Enter your Phone number:");
                        phone[i] = sc.nextInt();
                        sc.nextLine();

                        System.out.println("Enter your name:");
                        name[i] = sc.nextLine();

                        System.out.println("Enter your address:");
                        address[i] = sc.nextLine();

                        System.out.println("Enter your Pick up city:");
                        city[i] = sc.nextLine();

                        System.out.println("Enter your Destination:");
                        destination[i] = sc.nextLine();

                        System.out.println("Enter your Date:");
                        date[i] = sc.nextLine();
                        ++i;
                        Connection conn = null;
                        Statement stmt = null;
                        try{
                            //STEP 2: Register JDBC driver
                            Class.forName("com.mysql.jdbc.Driver");

                            //STEP 3: Open a connection
                            System.out.println("Connecting to a selected database...");
                            conn = DriverManager.getConnection(DB_URL, USER, PASS);
                            System.out.println("Connected database successfully...");

                            //STEP 4: Execute a query
                            System.out.println("Inserting records into the table...");
                            stmt = conn.createStatement();
                            String sql="INSERT INTO `registration`(phone,name,address,city,destination,date) VALUES('"+phone+"','"+name+"','"+address+"','"+city+"','"+destination+"','"+date+"')";
                            stmt.execureUpdate(sql); //this line will run the query
                        }catch(SQLException se){
                            //Handle errors for JDBC
                            se.printStackTrace();
                        }catch(Exception e){
                            //Handle errors for Class.forName
                            e.printStackTrace();
                        }finally{
                            //finally block used to close resources
                           try{
                                if(stmt!=null)
                                    conn.close();
                            }catch(SQLException se){
                            }// do nothing
                            try{
                                if(conn!=null)
                                    conn.close();
                            }catch(SQLException se){
                                se.printStackTrace();
                            }//end finally try
                        }//end try
                        System.out.println("Goodbye!");
                        break;
                    }
uqcuzwp8

uqcuzwp82#

据我所知你在构造弦 sql 但你不能在任何地方使用它。
以下代码用于执行查询: stmt.executeUpdate(sql); 有关这方面的详细信息,请参阅oracle-processing sql语句。
与您的问题无关,但准备好的语句也是您可能需要研究的内容,因为您通常会将用户输入直接插入数据库。用户的输入也可能是恶意的,这里有更多的解释-sql注入

l7mqbcuq

l7mqbcuq3#

你的 INSERT 语句应如下所示:

String sql = "INSERT INTO registration (phone, name, address, city, destination, date) VALUES ('" + phone[i] + "', '" + name[i] + "', '" + address[i] + "', '" + city[i] + "', '" + destination[i] + "', '" + date[i] +"');";

你应该打电话: stmt.executeUpdate(sql); 不是 executeQuery() 因为后者用于返回行而不是插入行

相关问题