Wolf87
Wolf87

Reputation: 540

Trying to connect to database in Java DB netbeans?

I'm trying to connect to embedded database Java DB from NetBeans 7.1.

this is what I tried:

    try{
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String url = "jdbc:derby:market; create=true";
    String db = "/artikli";
    String user = "wolf";
    String pass = "wolf";
    String query = "SELECT * FROM artikli";
    Connection conn = null;

        Class.forName(driver);
        conn = DriverManager.getConnection(url + db, user, pass);
        java.sql.Statement stmt = conn.createStatement();
        ResultSet res = stmt.executeQuery(query); 

        System.out.println(res.getString("naziv")); // naziv = column name


        res.close();

    }catch(Exception e){

    }

My question is how to get data and print, or populate JTable with it, and is this connection good, thanks? thank you in advance.

Upvotes: 0

Views: 935

Answers (2)

erikjber
erikjber

Reputation: 11

You must advance the result set to the first entry, like this:

while(res.next()){System.out.println(res.getString("naziv"));}

Upvotes: 1

Oleg Mihailenko
Oleg Mihailenko

Reputation: 91

look at your code, you even don't print expected errors, your catch block is empty, so print this exception and you will get the answer.

Upvotes: 0

Related Questions