1

我正在尝试将 SQLite 驱动程序添加sqlitejdbc-v056.jar到我的 netbeans 7.1.2 项目中,我已经创建了数据库和所有内容,但没有任何问题,但是当我尝试使用以下代码(我上网)连接到数据库时,它不起作用

public static void main(String args[]) throws Exception {
    Class.forName("org.sqlite.JDBC");

    Connection conn = DriverManager.getConnection("jdbc:sqlite:Vinit");

    Statement stat = (Statement) conn.createStatement();
    stat.executeUpdate("drop table if exists school;");
    stat.executeUpdate("create table school (name, state);");
    PreparedStatement prep = conn.prepareStatement(
                             "insert into school values (?, ?);");

    prep.setString(1, "UTD");
    prep.setString(2, "texas");
    prep.addBatch();
    prep.setString(1, "USC");
    prep.setString(2, "california");
    prep.addBatch();
    prep.setString(1, "MIT");
    prep.setString(2, "massachusetts");
    prep.addBatch();

    conn.setAutoCommit(false);
    prep.executeBatch();
    conn.setAutoCommit(true);

    ResultSet rs = stat.executeQuery("select * from school;");

    while (rs.next()) {
        System.out.print("Name of School = " + rs.getString("name") + " ");
        System.out.println("state = " + rs.getString("state"));
    }

    rs.close();
    conn.close();

stat.executeQuery给出以下错误给我一个问题

cannot find symbol
symbol:   method executeQuery(java.lang.String)
location: variable stat of type java.beans.Statement

有谁知道为什么会这样?

编辑:进口

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

当我添加import java.sql.Statement;它给它一个错误并说要删除未使用的导入

4

1 回答 1

2

检查Statement 接口是否是正确的,即。 java.sql.Statement;

在您的代码中,将import java.beans.Statement;错误地更改为import java.sql.Statement;

于 2012-07-30T11:39:31.613 回答