Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a school project, at home it works in netbeans when i connect the database using java derby, but moving it to another computer, it no longer knows where the database is located, Could someone point out the mistake and tell me how to correct it so it will look for the database in a folder in the .jar file?

public boolean checkLogin(String username, String password) {

       //This code will connect the database to the java program
       Connection myconObj = null; //allows to connect to database
       Statement mystatObj = null; // create statement (Execute queries)
       ResultSet myresObj = null; // get result
       ResultSetMetaData mymeta = null;

       try {

           String query = "select * from JACOVANSTRYP.MAINUSERDATA";

           myconObj = DriverManager.getConnection("jdbc:derby://localhost:1527/MainUserData", "jacovanstryp", "Password1234");
           mystatObj = myconObj.createStatement();
           myresObj = mystatObj.executeQuery(query);
           mymeta = myresObj.getMetaData();
           int colomnNo = mymeta.getColumnCount();

           while (myresObj.next()) {
               String dbUsername = myresObj.getString("Username");
               String dbPassword = myresObj.getString("Password");
               System.out.println();
               if (username.equalsIgnoreCase(dbUsername) && password.equals(dbPassword)) {

                   PrintWriter activeUser = new PrintWriter(new FileWriter("activeUser.db"));
                   activeUser.println(dbUsername);
                   activeUser.close();
                   return true;
               }
           }

       } catch (Exception e) {
           e.printStackTrace();
       }

       return false;
   }


What I have tried:

Creating a URL location to the database, it just says Driver not found
DriverManager.getConnection("jdbc:derby://Path/To/Jar/Folder/MainUserData", "jacovanstryp", "Password1234");
Posted
Updated 22-Feb-18 19:16pm
Comments
ThilinaMD 20-Feb-18 8:10am    
This link might help you https://netbeans.org/kb/docs/ide/java-db.html

1 solution

You need to give the server name in the connection string
instead of localhost, mention your database server name

here are some example
Building the Connection URL | Microsoft Docs[^]

According to this your connection string should be like this
Java
myconObj = DriverManager.getConnection("jdbc:derby://192.168.25.36:1527/MainUserData", "jacovanstryp", "Password1234");


or mention server name directly instead of IP
 
Share this answer
 
Comments
Maciej Los 23-Feb-18 3:03am    
5ed!
RDBurmon 23-Feb-18 4:36am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900