0

I have drive derbyclient.jar in my libraries but it's still not finding my database. I just can't connect to database.

String host = "jdbc:derby://localhost:1527/prosto";
String uName = "username";
String uPass = "password";

Connection con = DriverManager.getConnection(host, uName, uPass);
Osman Omar
  • 433
  • 1
  • 7
  • 19

2 Answers2

3

You will first need to load the derby driver class. To do that, add this code before the DriverManager.getConnection() call.

try{
    Class.forName("org.apache.derby.jdbc.ClientDriver");// or may be it is "org.apache.derby.jdbc.EmbeddedDriver"? Not sure. Check the correct name and put it here.
} catch(ClassNotFoundException e){
    //handle exception
}

This will load and register the Derby driver class in the JDBC's driver registry, after which you'll be able to connect to the database.

Refer to this for more details:

https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html

Update

There should be a derbyclient.jar in the lib folder of derby installation. You will need to add that also to the class path and make it available at run time. This seems to solve the problem for me.

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
0
It simole example for you:

    import java.sql.*;

public class FirstExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/EMP";  
//You must use driver, but you can use this driver:
// static String DB_URL = "com.mysql.cj.jdbc.Driver";


   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT id, first, last, age FROM Employees";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
      }
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{

      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}//end main
}


Также хороший совет- читай и пиши на английском, IDE также свою переведи на английский (а вообще советую Intellij idea, у которой, кстати есть бесплатные лицензии для школьников и студентов.).

Вот ссылка на статью о смене языка в NetBeans http://blog.matros.com.ua/%D0%BA%D0%B0%D0%BA-%D0%B8%D0%B7%D0%BC%D0%B5%D0%BD%D0%B8%D1%82%D1%8C-%D1%8F%D0%B7%D1%8B%D0%BA-%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D1%84%D0%B5%D0%B9%D1%81%D0%B0-%D0%B2-netbeans-7-3-%D0%BD%D0%B0-%D0%B0%D0%BD/

User
  • 101
  • 2
  • 6