I'm trying to design a desktop application in Java and MySQL. The application deals with orders, invoices, etc. For the moment it doesn't connect to Internet. There are 9 terminals that need to connect with the server to make queries to the database.
Question : How should I make the connections from the client terminals to the server? I guess it`s not by hard coding in every terminal the IP address of the server in the LAN.
If you are intending to create a standalone JAVA application, then simply pass the DBMS server connection properties such as host , port (may not need this parameter if you are walking with default one of MySQL server), username and password then use those arguments to create your connection String
url.
As a sample, consider the following main
method as your application entry point:
public static void main(String[] args) {
String host = args[0];
String port = args[1];
String username = args[2];
String password = args[3];
String dbName = "db_name";
try {
// Load the MySQL driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
Connection connect = DriverManager
.getConnection("jdbc:mysql://"
+ host
+ ":" + port
+ "/" + dbName + "?"
+ "?user=" + username + "&password=" + password);
// Use that conncetion
} catch (Exception e) {
// Catch connection exceptions
} finally {
// Close connection
}
Then the invocation from command line would be with a packaged artifact:
java -jar packaged-application.jar localhost 3306 root rootroot
Or using your the class where your main
resides:
java MainClass localhost 3306 root rootroot
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.