如何让基于Derby JDBC的Java微型酿酒项目在任意电脑运行?
Hey there! Let's break down why your micro-brewing system won't connect to the database on other PCs and walk through practical fixes to make it run anywhere.
The Root Problem
Your current JDBC connection uses Derby's Client/Server mode:
con = DriverManager.getConnection("jdbc:derby://localhost:1527/BrewingDatabase", "userID", "password");
This URL points to a Derby server running locally on your machine. Other PCs don't have that server running, nor do they have a copy of your BrewingDatabase files—so the connection fails every time.
Solution 1: Switch to Derby Embedded Mode (Best for Portable Apps)
Derby's embedded mode lets your program access the database directly from files, no separate server required. This is perfect for a self-contained micro-system.
Update your JDBC URL to use embedded mode:
con = DriverManager.getConnection("jdbc:derby:BrewingDatabase;create=true", "userID", "password");The
create=trueflag will create the database if it doesn't exist (great for first-time runs on new PCs).Package the Derby driver with your app:
In NetBeans, go to your project's Properties > Libraries, add the Derby embedded driver (usually namedderby.jar), and make sure it's included when you package your project. When you export your app as an executable JAR, select the option to copy dependencies to the same folder or package them inside the JAR.Copy the database files:
Your existingBrewingDatabasefolder (usually stored inC:\Users\YourName\.derbyor NetBeans' designated data directory) needs to be placed in the same folder as your executable JAR on other PCs.
Solution 2: Use a More Portable Database (SQLite)
If you want even simpler portability, switch to SQLite—it's a file-based database that requires no server at all, just a single .db file.
Add the SQLite JDBC driver to your NetBeans project (search for
sqlite-jdbcin Maven or download the JAR directly).Update your connection code:
con = DriverManager.getConnection("jdbc:sqlite:BrewingDatabase.db");SQLite doesn't require usernames/passwords by default (you can add them if needed).
Distribute the
.dbfile:
Just copy yourBrewingDatabase.dbfile along with your app's JAR—other PCs will access it directly without any setup.
Bonus: Avoid Hardcoded Credentials
To make your app even more flexible, store database settings in a config file instead of hardcoding them.
Create a
config.propertiesfile in your project root:db.url=jdbc:derby:BrewingDatabase;create=true db.user=userID db.password=passwordLoad the config in your Java code:
Properties dbProps = new Properties(); dbProps.load(new FileInputStream("config.properties")); String url = dbProps.getProperty("db.url"); String user = dbProps.getProperty("db.user"); String pass = dbProps.getProperty("db.password"); con = DriverManager.getConnection(url, user, pass);Now you can tweak settings on different PCs without editing your code.
Final Checks
- Always test your packaged app on a fresh PC to confirm dependencies and database files are included.
- Make sure the target PC has Java installed (match the Java version you used to build the app).
内容的提问来源于stack exchange,提问作者Vittal Kamath

