Java SQL error, no suitable driver found

I am trying to compile this small piece of code to help me connect to my db and get some information to check it. I am using Netbeans on a Windows 7 x64 computer. This is the code:

package passwordprotector; import java.sql.*; public class PasswordProtector { /** * @param args the command line arguments */ public static void main(String[] args) { String host = "jdbc:derby://localhost:1527/PasswordProtector DB"; String dbUsername = "john"; String dbPassword = "arsenal"; /*try{ Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); }catch(ClassNotFoundException e){ System.out.println(e); }*/ try{ Connection con = DriverManager.getConnection(host, dbUsername, dbPassword); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM APP.PERSON"); while (rs.next()) { String uName = rs.getString("uname"); String uPass = rs.getString("upass"); System.out.println("Username: " + uName + "/n" + "Password: " + uPass); } }catch(SQLException e){ System.err.println(e); } } } 

When I compile and run, I get this error:

 java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/PasswordProtector DB BUILD SUCCESSFUL (total time: 0 seconds) 

When I right-click on my db and select properties, I can see its location, for example:

 Database URL: jdbc:derby://localhost:1527/PasswordProtector 

I checked with others who posted about it, and it looks like they had the wrong URL, but I don't see another URL that I can use separately from the published one.

I tried with and without end "DB" for the String host, not working.

I also read from here and still could not understand why URl is incorrect:

+1
source share
3 answers

Not sure if the problem is with connecting the database url but using the correct driver. If the database is embedded, you should use the driver commented in your post and example from my answer , there is also a built-in derby .

if not, use

 Class.forName("org.apache.derby.jdbc.ClientDriver"); 

This is another driver for connecting to a stand-alone database. In this case, see the derby network client documentation for setting up and running the derby network client.

+2
source

Make sure derbyrun.jar is in your class path. It is located in the db / lib directory of your JDK.

+2
source

Doing a quick search on Maven and Derby included the following in my pom:

 <dependency> <groupId>org.apache.derby</groupId> <artifactId>derbyclient</artifactId> <version>10.10.2.0</version> </dependency> 

and after that everything worked, so it could be a library problem if the previous solution does not work.

0
source

All Articles