sql数据库中的java.lang.NullPointerException

时间:2016-04-12 07:58:44

标签: java sql search

我想在数据库中存储一个值,如果它不存在,如果不是我想插入但我遇到了问题:

  

显示java.lang.NullPointerException ..

String read = "flavour"; 

public static void dataStore(String read) {

    try {

        Connection connect = null;
        ResultSet rs = null;
        PreparedStatement st = null;

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        // Setup the connection with the DB
        connect = DriverManager.getConnection("jdbc:sqlserver://WMDENTW1\\SQLEXPRESS:1433;" + "database=FullTextDB;"
                + "user=root;" + "password=root123");
        String key = "IF EXISTS (SELECT * FROM QueryStrings WHERE Query_String= '"+read+"') UPDATE QueryStrings SET Query_Count=Query_Count + 1 WHERE Query_String ='"+read+"' ELSE INSERT INTO QueryStrings VALUES ('"+read+"', 1)";

        st.executeQuery(key);
        System.out.println("data commited");
        connect.commit(); 

    } catch(Exception e) {
        System.out.println(e);
    }
}

1 个答案:

答案 0 :(得分:1)

当您致电PreparedStatement st时,st.executeQuery()为空。您需要创建一个语句,例如st = conn.createPreparedStatement(key);

或者,您可以创建一个简单的声明:

Statement st = conn.createStatement();
st.executeQyery(key);