JAVA更新数据库 - 错误

时间:2016-08-23 18:11:32

标签: java sql database sqlite

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.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)

有什么问题?当我从数据库中读取时,一切正常,但更多功能是错误的。

无法正常更新和插入 .................................................. .................................................. .................................................. .................................................. .........................................

1 个答案:

答案 0 :(得分:4)

您正尝试使用executeQuery()方法执行SQL UPDATE语句。 executeQuery()不能获取更新数据库的语句,例如UPDATE,INSERT或DELETE。将此行替换为:

int rowsUpdated = statement.executeUpdate(sql);