1

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

1 Answer 1

1

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

    ....

}
Sign up to request clarification or add additional context in comments.

3 Comments

I have removed try catch block and now I get this in the console:
Exception in thread "main" java.sql.SQLException: out of memory at org.sqlite.DB.throwex(DB.java:288) at org.sqlite.NestedDB._open(NestedDB.java:73) at org.sqlite.DB.open(DB.java:77) at org.sqlite.Conn.<init>(Conn.java:88) at org.sqlite.JDBC.connect(JDBC.java:64) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at GestorBase.main(GestorBase.java:21) Any idea?:_
You have an "out of memory" there. Try starting Java with the -Xmx option, for example -Xmx512m.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.