开发者

SQLite database connection problem Java

开发者 https://www.devze.com 2023-01-28 23:27 出处:网络
I am trying to do some querys to a database I created but I am having problems connecting to it: enter code here import java.sql.Connection;

I am trying to do some querys to a database I created but I am having problems connecting to it:

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);
        }



    }
}

And the console message that I get is:

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

The line 27 is the " Statement sentencia = con.createStatement(); " one, so I suppose that tha开发者_如何学Gots the nullpointer exception generator? but I also get the "error al buscar en la base de datos" message,witch is the one inside the catch block and that means that there is a problem with the " Statement sentencia = con.createStatement(); " line too right? anyone?:_


You have an error in the con=DriverManager.getConnection line, masked by the System.out.println("error al buscar la base de datos"); instruction. The other error is cause by the first one, just ignore it. To see what the real error is, just remove the try ... catch around the getConnection call, like this:

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();

    ....

}
0

精彩评论

暂无评论...
验证码 换一张
取 消