JDBC:MYSQL works over LAN, but does not work over internet
843859Apr 5 2007 — edited Apr 26 2007Hi,
I have to use a database in my project
So decided upon using JDBC and MySQL 5.0.37
I downloaded the connector/J 5.0 jar files as well and used it
The problem i face is that, The client can access the database when its on LAN
But the same code with updated URL does not work over the internet.
This problem also troubles me in my client-server program as well.
I would like to know whether its coz of NAT or something to with my ISP
I also do use a firewall - McAffee
I tried putting off the firewall as well, but same problem
NetBeans 5.5 VMOptions : -Djava.security.policy=applet.policy
DB : MySQL 5.0.37 - server will be running
connector : mysql-connector-java-5.0.5-bin.jar
The client code is as follows :
import java.sql.*;
public class DBTest {
String URL;
String username;
String password;
String Query;
Statement stmt;
Connection con;
ResultSet res;
/** Creates a new instance of DBTest */
public DBTest() {
}
public void DB(){
//Set Values
URL = "jdbc:mysql://localhost/beergame"; //( works )
// URL = "jdbc:mysql://125.164.121.2/beergame";( dynamic ip - doesnt work )
username = "root";
password = "pass";
stmt = null;
con = null;
try{
LoadDriver();
ConnectToDB();
ExecQuery();
CloseConnection();
} catch (Exception e) {
System.err.println("problems with SQL sent to "+URL+
": "+e.getMessage());
}
}
public void LoadDriver(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println("Failed to load Connector/J");
return;
}
}
public void ConnectToDB(){
try {
con = java.sql.DriverManager.getConnection(
URL,
username,
password);
stmt = con.createStatement();
} catch (Exception e) {
System.err.println("problems connecting to "+URL);
}
}
public void ExecQuery(){
// execute SQL commands to create table, insert data
try{
//execute query
Query = "SELECT * FROM test;";
res = stmt.executeQuery(Query);
// While more rows exist, print them
while (res.next() ) {
// Use the getString method to obtain test. name
System.out.println("Name : " + res.getString("name"));
System.out.println();
}
} catch (Exception e) {
System.err.println("problems executing query at "+URL);
}
}
public void CloseConnection(){
//close connection once done
try{
con.close();
} catch (Exception e){
System.err.println("Error closing connection to "+URL);
}
}
public static void main(String[] args){
DBTest obj = new DBTest();
obj.DB();
}
}
Thanks in advance