PDA

View Full Version : Java JDBC Example - Basic Database Operation



Mitch1234
06-15-2009, 05:53 PM
If the code below were on an applet launched remotely it would not work because the localhost does not contain the DB. What would the proper "String url =" be to connect remotely to my DB for my site.

Site: www.novajobseeker.com
DB name: novajobs_customer
Table name: CustomerInfo

Please anyone that knows how to connect remotly within java please respond. Thanks :D

Also will I need to add "Remote Database Access Hosts". The remote connection will just be any IP on the internet.



import java.sql.*;

public class JDBCDemo {
public static void main( String[] args ) {
try {
// Connect to the database
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost/northwind";
Connection con = DriverManager.getConnection(url, "USERNAME", "PASSWORD");
// Execute the SQL statement
Statement stmt = con.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * from customers");
System.out.println("Got results!");
// Loop thru all the rows
while( resultSet.next() ) {
String data = resultSet.getString( "CompanyName" );
System.out.println( data );
}
stmt.close();
}
catch( Exception e ) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

Mitch1234
06-15-2009, 06:02 PM
Never Mind found the solution.

I just need to find my shared server IP and make sure my home IP is whitelisted.


Solution:

There are two different ways to connect to a MySQL database. Locally (from a script on the server, to the server), and Remotely (from a remote computer, using Shell, ODBC, or Navicat)

Locally, the settings are:
Hostname: localhost
Username: username_dbuser
Password: dbpassword
db_name: username_dbname
db_table: dbname_table

Remotely, the settings are the same, but you must use the Shared Server IP in place of 'localhost'. As well, your IP address (which has been assigned to you by your Internet Service Provider) must be whitelisted on our end, to get through our firewall. You can either call us, or send us an email to support@hostmonster.com, with your domain name and IP address to be whitelisted.

Mitch1234
06-15-2009, 06:08 PM
If the IP's are not static I will still have issues connecting because of whitelisting.

So would anyone recomened me creating scripts on the server to access the DB and have the application call the scripts?

anonymous
06-18-2009, 04:36 PM
If the IP's are not static I will still have issues connecting because of whitelisting.

if you're talking about your ip changing, you can always ask HM to
whitelist the first 2 parts of your IP and then wildcard.

I'm on AT&T dsl and my ip is 71.145.163.187
it changes all the time. Maybe HM will whitelist
my IP like 71.145.*.*? I'm sure 71.145 only belongs to at&t.

shadmego
06-18-2009, 05:00 PM
if you're talking about your ip changing, you can always ask HM to
whitelist the first 2 parts of your IP and then wildcard.

I'm on AT&T dsl and my ip is 71.145.163.187
it changes all the time. Maybe HM will whitelist
my IP like 71.145.*.*? I'm sure 71.145 only belongs to at&t.

You can probably check this at ws.arin.net

Mitch1234
06-20-2009, 03:14 AM
Thanks all I was able to get it working.