0

When I try to create an instance of DB in an other class, in FirstConnection. In the constructor at first I try to create a connection: con. Then I try to create a statmenet: createstatement. Then I get the database meta data: dbmd, because with that I tray to check whether the database is empty or not: from dbmd I get the result set rs1 than with that I check rs1.next(), If it is false I call "createstatement.execute("create table users (name varchar(20), adress varchar(20)")" to create a table. When I try to create an instance in FirstConnection I get this exception: "java.sql.SQLException: No suitable driver found for jdbc:derby:sampleDB;create=true". Do you have any idea what the problem could be? I do this in EclipseEE. could that be the problem? I am new to java JDBC. Thank you!

public class FirstConnection {
    public static void main(String[] args) {
        DB db=new DB();
    }
}

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {

    String url = "jdbc:derby:sampleDB;create=true";
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String username = "";
    String password = "";

    public DB() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(url);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Statement createstatement = null;
        if (con != null) {
            try {
                createstatement = con.createStatement();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        DatabaseMetaData dbmd = null;
        try {
            dbmd = con.getMetaData();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            ResultSet rs1 = dbmd.getTables(null, "APP", "USERS", null);
            if (!rs1.next()) {
                createstatement.execute("create table users (name varchar(20),             adress varchar(20)");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

} ```
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
gerg
  • 19
  • 1
  • 6
  • Does this answer your question? [SQLException: No suitable driver found for jdbc:derby://localhost:1527](https://stackoverflow.com/questions/3816015/sqlexception-no-suitable-driver-found-for-jdbcderby-localhost1527) – rkosegi Feb 24 '21 at 11:24
  • What version of Derby are you using? What version of Java are you using? What is in your classpath? What is the *exact*, *complete* exception that you get? – Bryan Pendleton Feb 24 '21 at 16:11
  • the complete exception lokks like this: java.sql.SQLException: No suitable driver found for jdbc:derby:sampleDB;create=true at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:270) at DB.(DB.java:18) at FirstConnection.main(FirstConnection.java:4) – gerg Feb 24 '21 at 18:51
  • It was my fault: I downloaded the wrong version of derby: I downloaded the version for Java 10, but I had Java 8 on my machine.But thank you for your help! – gerg Mar 17 '21 at 10:39

0 Answers0