简体   繁体   English

如何从可执行jar文件访问derby数据库?

[英]How to access derby database from executable jar file?

I am using java and derby database. 我正在使用Java和derby数据库。 I built derby database in netbeans IDE. 我在netbeans IDE中构建了derby数据库。 I have created a executable jar file using clean and build option. 我已经使用clean and build选项创建了一个可执行的jar文件。

While running the project in netbeans, it connect to database goodly. 在netbeans中运行项目时,它可以很好地连接到数据库。 But if I run executable jar file, it doesn't connect to the database. 但是,如果我运行可执行jar文件,它就不会连接到数据库。

Help me to create a executable jar file with derby database, It should run on any other system.Part of coding to connect database is 帮助我用derby数据库创建一个可执行jar文件,它应该在任何其他系统上运行。连接数据库的部分编码是

String str = textarea.getText();
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,userName,password);
    String stm="select url from pingatabl where functn=?";
    PreparedStatement st = conn.prepareStatement(stm);
    st.setString(1, str);
     //Excuting Query
    ResultSet rs = st.executeQuery();
    if (rs.next()) {
    String s = rs.getString(1);
    //Sets Records in frame
    JFrame fm = new JFrame();
    fm.setVisible(true);
    fm.setSize(500,750);
    JEditorPane jm = new JEditorPane();
    fm.add(jm);
    jm.setPage(ClassLoader.getSystemResource(s));

You are as novice as I was 2 years ago. 您像我两年前一样是新手。 The thing is that you have not installed derby at all. 问题是您根本没有安装derby。 See, Derby is a database server and netbeans has its own version of derby. 可以看到,Derby是数据库服务器,而netbeans具有自己的derby版本。 So, it runs when made to run inside Netbeans but does not start when run from an executable file. 因此,它可以在Netbeans内部运行时运行,但从可执行文件运行时则不会启动。 So, first, install derby from https://db.apache.org/derby/ and then start the server and then you can access it from the executable file. 因此,首先,从https://db.apache.org/derby/安装derby,然后启动服务器,然后可以从可执行文件访问它。 Also, I would recommend you to use MySQL server instead of derby. 另外,我建议您使用MySQL服务器而不是derby。 But, you can always use derby. 但是,您始终可以使用derby。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM