SQLite数据库连接问题Java

时间:2010-12-06 22:04:52

标签: java sql eclipse

我正在尝试对我创建的数据库进行一些查询但是我在连接它时遇到问题:

enter code here import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class GestorBase{
private  ResultSet resultset;
private static  Connection con;
private  Statement sentencia;

public static void main(String[] args) throws SQLException, ClassNotFoundException{

    Class.forName("org.sqlite.JDBC");




    try {
        con=DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite");
    } catch (SQLException e) {

        System.out.println("error al buscar la base de datos");
    }

        Statement sentencia = con.createStatement();




    String query = "SELECT * FROM Restaurantes";

    ResultSet resultset = sentencia.executeQuery(query);





        while(resultset.next())
        {
            String nombre = resultset.getString("NOMBRE");
            String calle = resultset.getString("CALLE");
            int codigo = resultset.getInt("CODIGO");
            System.out.println("Codigo de restaurante:   "+codigo+"Nombre de restaurante: "+ nombre +"
        Calle del restaurante: "+ calle);
        }



    }
}

我得到的控制台消息是:

error al buscar la base de datos
Exception in thread "main" java.lang.NullPointerException
at GestorBase.main(GestorBase.java:27)

第27行是“Statement sentencia = con.createStatement();”1,所以我想这就是nullpointer异常生成器?但我也得到了“错误al buscar en la base de datos”的消息,女巫是catch块内的那个,这意味着“Statement sentencia = con.createStatement();”行有问题吗?任何人:_

1 个答案:

答案 0 :(得分:1)

con=DriverManager.getConnection行中有错误,被System.out.println("error al buscar la base de datos");指令屏蔽。另一个错误是由第一个引起的,只是忽略它。 要查看真正的错误,只需移除try ... catch电话周围的getConnection,如下所示:

public static void main(String[] args) throws SQLException, ClassNotFoundException{

    Class.forName("org.sqlite.JDBC");
    con=DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite");
    Statement sentencia = con.createStatement();

    ....

}