我怎么改变这个,所以我会在任何计算机上工作? [英] How would I change this so I would work on any computer?

查看:84
本文介绍了我怎么改变这个,所以我会在任何计算机上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个学校项目,在家里,当我使用java derby连接数据库时,它在netbeans中运行,但是将它移动到另一台计算机,它不再知道数据库的位置,有人可以指出错误和告诉我如何更正它以便在.jar文件中的文件夹中查找数据库?



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;
   }





我尝试过:



创建数据库的URL位置,它只是说找不到驱动程序



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");

推荐答案

您需要提供服务器名称在连接字符串中

而不是localhost,提及你的数据库服务器名称



这里有一些例子

< a href =https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url>构建连接URL | Microsoft Docs [ ^ ]



根据这个你的连接字符串应该是这样的

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
myconObj = DriverManager.getConnection("jdbc:derby://192.168.25.36:1527/MainUserData", "jacovanstryp", "Password1234");





或直接提及服务器名称而不是IP



or mention server name directly instead of IP


这篇关于我怎么改变这个,所以我会在任何计算机上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆