|
How to Run Java Program With
Database On Another Computer?
use JBDC. It doesn't matter where your database. import java.sql.*; public class Connect { public static void main (String\[\] args) { Connection conn = null; try { String userName = "testuser"; String password = "testpass"; String url = "jdbc:mysql://localhost/test"; Class.forName ("com.mysql.jdbc.Driver").newInstance (); conn = DriverManager.getConnection (url, userName, password); System.out.println ("Database connection established"); } catch (Exception e) { System.err.println ("Cannot connect to database server"); } finally { if (conn != null) { try { conn.close (); System.out.println ("Database connection terminated"); } catch (Exception e) { /* ignore close errors */ } } } } }
Just replace localhost with IP address of host where MySql server runs. Or You can do this by using sockets to transfer your query from one computer from other and then execute that query at the other platform. Or Install your mysql in remote machine A and setup your JVM in machine B. And try to access the remote database by specifying the IP in the connection url. Or You might also consider configuring your database server to allow remote connections. On some hosts, this feature is disabled by default as a security measure. |
|
See also
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|