2016-08-23 88 views
-2
package application; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public abstract class Query { 

    private static final String DRIVER_CLASSNAME = "org.sqlite.JDBC"; 
    private static final String PASSWORD = ""; 
    private static final String USERNAME = ""; 

    protected static String base = "data/pezalDB"; 
    private static String JDBC_URL = "jdbc:sqlite:" + base + ".db"; 

    protected static Connection connection; 
    protected static Statement statement; 
    public static ResultSet resultSet; 

    public static void connectToDatabase() throws ClassNotFoundException, SQLException { 
     Class.forName(DRIVER_CLASSNAME); 
     connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD); 
    } 

    public static void executeSql(String sql) throws SQLException { 

     statement = connection.createStatement(); 
     resultSet = statement.executeQuery(sql); 
    } 

    public static void close() throws SQLException { 
     resultSet.close(); 
     statement.close(); 
     connection.close(); 
    } 

} 

public class TableDB { 

    private int id; 
    private String namePL; 
    private String nameEN; 

    public TableDB(String namePL, String nameEN) { 
     this.namePL = namePL; 
     this.nameEN = nameEN; 
    } 
} 

public class QueryMethods extends Query { 

    static String table = "dictionary"; 
    static String sql; 

    public static void addValue() { 
     try { 
      connectToDatabase(); 

      sql = "UPDATE dictionary SET namePL = 'QWE' WHERE id = 2"; 

      executeSql(sql); 
      close(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 

     addValue(); 

    } 
} 

我有一個問題:JAVA更新數據庫 - 錯

java.sql.SQLException: query does not return ResultSet 
    at org.sqlite.jdbc3.JDBC3Statement.executeQuery(JDBC3Statement.java:85) 
    at application.QueryMethods.addValue(QueryMethods.java:18) 
    at application.TestQuery.main(TestQuery.java:10) 

有什麼不對?當我從數據庫中讀取一切正常,但更多的功能是錯誤的。

沒有工作更新並插入 ....................................... .................................................. .................................................. .................................................. .................................................. ..

+0

您能否提供SQL查詢?我的猜測是那裏有一個問題。也許你沒有執行SELECT語句,而是執行更新/插入。這將解釋它。 – sagneta

回答

4

您正嘗試使用executeQuery()方法執行SQL UPDATE語句。 executeQuery()不能接收更新數據庫的語句,如UPDATE,INSERT或DELETE。將此行替換爲:

int rowsUpdated = statement.executeUpdate(sql);