0

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!

Brian
  • 7,955
  • 16
  • 66
  • 107
  • 2
    have you tried adding port number to connection string? – Harry Joy Jan 06 '11 at 07:34
  • 1
    Check the login history to see if the connection reaches the server. On linux you would do this: Mysql login logs can be enabled by starting mysql server with the option --log="log-file" option. /usr/bin/safe_mysqld --log="/var/lib/mysql/mysql.log" & Mysql log will be logged inside the file /var/lib/mysql/mysql.log. tail this file to see the logs. To see only the login (connect log) use the following command grep Connect /var/lib/mysql/mysql.log – Koekiebox Jan 06 '11 at 07:47

4 Answers4

2

Try following:

public class MySqlDemo {

public static void main(String [] args) {

        java.sql.Connection conn = null;

        System.out.println("SQL Test");

        try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                conn = java.sql.DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test?user=root&password=");

        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
                }

        System.out.println("Connection established");
}
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • +1 because you were the first one to comment about the port number. :) – Adeel Ansari Jan 06 '11 at 08:04
  • Hey I tried your method and it looks like it's making progress. Now I'm getting a "Connection refused" exception when I run the thing. Is there something I have to do on the server, such as permissions? If so where do I look or what should I do. I'm running the server from a Debian machine if that's helpful. – Brian Jan 08 '11 at 05:10
  • Make sure your mysql service is running and its on 3306 port. Your db name is correct. Username & Password is correct. – Harry Joy Jan 08 '11 at 07:05
1

You have to provide the name of the Schema to which you are connecting. Usually, the port is also added.

This is a sample connection string:

jdbc:mysql://repos.insttech.washington.edu:3306/johndoe?user=johndoe&password=jddb

3306 is the port and the first instance of johndoe is the name of the Schema. The second instance of johndoe is the username.

npinti
  • 51,780
  • 5
  • 72
  • 96
0
  1. Try putting port number and schema there
  2. Try logging into database using some SQL client, may be SQL console
  3. Try other drivers, may be some newer or perhaps older
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
0

It could be that the Connector/J library is trying to use a named pipe to connect to the MySQL server rather than using TCP/IP and for some reason the named pipe isn't available. Try specifying a port number.

You may also want to try turning on some logging in Connector/J's configuration as described here.

sjr
  • 9,769
  • 1
  • 25
  • 36