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 thats 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?:_