General JDBC Setup

Posted by AeroDroid on Stack Overflow See other posts from Stack Overflow or by AeroDroid
Published on 2011-01-06T07:26:52Z Indexed on 2011/01/06 7:53 UTC
Read the original article Hit count: 295

Filed under:
|
|

So I have a MySQL database set up on a Debian server and it works fine from a phpMyAdmin client. I'm currently working on a project to write a Java server that would be able to use the MySQL database that is already on this server through a JDBC connection. I've looked at many tutorials and documentations but all of them seem to just explain how to do client-side code, but I have yet to figure out how to even successfully open a JDBC connection to the server. As far as I am concerned, I believe that program has the drivers properly set up because it's not crashing anymore (I simply direct the Java Build Path of my program to the Connector/J provided by MySQL). As far as my program goes, this is what it looks like...

import java.sql.*;

public class JDBCTest {
    public static void main(String[] args) {
        System.out.println("Started!");
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            System.out.println("Driver registered. Connecting...");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "password");
            System.out.println("Connected!");
            conn.close();
        } catch (SQLException e) {
            System.out.println("Error!");
            e.printStackTrace();
        }
    }
}

This is what's printed...

Started!
Driver registered. Connecting...

It's as if the DriverManager.getConnection(String) just freezes there. I'm sure this is a problem with the server because when I intentionally misspell localhost, or an IP address, the program crashes within 20 seconds. This just hangs there forever.

Sorry about this wall of text, but my final question is if anyone has any information what I should do or install on the server to get this to work? Thank you so much!

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql