JDBC插入查询未在数据库中插入记录

时间:2018-12-12 23:48:12

标签: java database

我有一个小问题。我的数据库无法正常工作,我也不知道为什么。没有错误。一切看起来都很好,我正在键入值,而我的Web应用程序却说“保存到数据库中”,这是不正确的。数据库为空。 这是我的代码:

public class ConnectionManager {

    private static String dbURL = "jdbc:derby://localhost:1527/CarRental";
    private static Connection conn = null;
    private static Statement stmt = null;

    public void createConnection() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            conn = DriverManager.getConnection(dbURL,"GioRgio","12345");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void addClient(Klienci klienci) {

        String query = "INSERT INTO KLIENCI"
                + "(ID, IMIE, NAZWISKO, ADRES, TELEFON, MAIL)"
                + " VALUES (" + klienci.getId_klienta() + ",'" + klienci.getImie() + "','" 
                + klienci.getNazwisko() + "'," + klienci.getAdres()+"',"+klienci.getTelefon()+"',"+klienci.getMail() + ")"; 

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
            stmt.close();
        } catch (SQLException sqlExcept) {
            sqlExcept.printStackTrace();
        }
    }
    public void closeConnection() {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                DriverManager.getConnection(dbURL + ";shutdown=true");
                conn.close();
            }
        } catch (SQLException sqlExcept) {
        }

    }
}

2 个答案:

答案 0 :(得分:2)

此问题的直接原因似乎是INSERT语句(地址)中错误地转义的文字。我们可以尝试解决此问题,但是最好使用准备好的语句来处理转义本身。

String sql = "INSERT INTO KLIENCI (ID, IMIE, NAZWISKO, ADRES, TELEFON, MAIL) ";
sql += "VALUES (?, ?, ?, ?, ?, ?,)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, klienci.getId_klienta());
ps.setString(2, klienci.getImie());
ps.setString(3, klienci.getNazwisko());
ps.setString(4, klienci.getAdres());
ps.setString(5, klienci.getTelefon());
ps.setString(6, klienci.getEmail());

ps.executeUpdate();

答案 1 :(得分:1)

Answer by Tim Biegeleisen关于错误输入SQL文本以及有关使用准备好的语句的建议是正确的。

示例应用

为支持他的答案,这里有一个完整的示例应用程序。使用H2 Database Engine而不是Derby

package com.basilbourque.example;

import java.sql.*;
import java.util.UUID;

public class CustomerDbEx {

    public static void main ( String[] args ) {
        CustomerDbEx app = new CustomerDbEx();
        app.doIt();
    }

    private void doIt ( ) {

        try {
            Class.forName( "org.h2.Driver" );
        } catch ( ClassNotFoundException e ) {
            e.printStackTrace();
        }

        try (
                Connection conn = DriverManager.getConnection( "jdbc:h2:mem:customer_example_db;DB_CLOSE_DELAY=-1" ) ; // Set `DB_CLOSE_DELAY` to `-1` to keep in-memory database in existence after connection closes.
                Statement stmt = conn.createStatement() ;
        ) {
            String sql = "CREATE TABLE customer_ ( \n" +
                    "  id_ UUID NOT NULL PRIMARY KEY , \n" +
                    "  given_name_ VARCHAR NOT NULL , \n" +
                    "  surname_ VARCHAR NOT NULL , \n" +
                    "  address_ VARCHAR NOT NULL , \n" +
                    "  phone_ VARCHAR NOT NULL , \n" +
                    "  email_ VARCHAR NOT NULL \n" +
                    ");";
            stmt.execute( sql );

            // Insert rows.
            sql = "INSERT INTO customer_ ( id_ , given_name_ , surname_ , address_ , phone_ , email_ ) \n";
            sql += "VALUES ( ? , ? , ? ,  ? , ? , ?  ) \n";
            sql += ";";
            try (
                    PreparedStatement preparedStatement = conn.prepareStatement( sql ) ;
            ) {
//                preparedStatement.setObject( 1 , customer.getId() );
//                preparedStatement.setString( 2 , customer.getGivenName() );
//                preparedStatement.setString( 3 , customer.getSurname() );
//                preparedStatement.setString( 3 , customer.getAddress() );
//                preparedStatement.setString( 3 , customer.getPhone() );
//                preparedStatement.setString( 3 , customer.getEmail() );
//                preparedStatement.executeUpdate();

                preparedStatement.setObject( 1 , UUID.fromString( "ddbf2754-f9aa-4ec3-98e9-b03da4aa83d1" ) );
                preparedStatement.setString( 2 , "Wendy" );
                preparedStatement.setString( 3 , "Melvoin" );
                preparedStatement.setString( 4 , "101 Main ST" );
                preparedStatement.setString( 5 , "(525) 555-1911" );
                preparedStatement.setString( 6 , "wendy@example.com" );
                preparedStatement.executeUpdate();

                preparedStatement.setObject( 1 , UUID.fromString( "5851c90a-f1cb-4706-a329-c54890e4d190" ) );
                preparedStatement.setString( 2 , "Lisa" );
                preparedStatement.setString( 3 , "Coleman" );
                preparedStatement.setString( 4 , "787 Dream ST" );
                preparedStatement.setString( 5 , "(525) 555-7824" );
                preparedStatement.setString( 6 , "lisa@example.com" );
                preparedStatement.executeUpdate();


            }

            // Query all.
            sql = "SELECT * FROM customer_ ;";
            try ( ResultSet rs = stmt.executeQuery( sql ) ; ) {
                while ( rs.next() ) {
                    //Retrieve by column name
                    UUID id = rs.getObject( "id_" , UUID.class );
                    String givenName = rs.getString( "given_name_" );
                    String surname = rs.getString( "surname_" );
                    String address = rs.getString( "address_" );
                    String phone = rs.getString( "phone_" );
                    String email = rs.getString( "email_" );

                    System.out.println( "Customer: " + id + " | " + givenName + " | " + surname + " | " + address + " | " + phone + " | " + email );

                    // Instantiate a `Customer` object for this data.
//                    Customer c = new Customer( id , givenName , surname , address , phone , email );
                }
            }
        } catch ( SQLException e ) {
            e.printStackTrace();
        }

    }

}

运行时。

  

客户:ddbf2754-f9aa-4ec3-98e9-b03da4aa83d1 |温迪| Melvoin | 101 Main ST | (525)555-1911 | wendy@example.com

     

客户:5851c90a-f1cb-4706-a329-c54890e4d190 |丽莎|科尔曼| 787 Dream ST | (525)555-7824 | lisa@example.com