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