Find the answer to your Linux question:
Results 1 to 1 of 1
Can anyone give me a suggestion about this simple test code? --------------------------------------------------------------------------------------------- import java.sql.*; public class sql { public static void main (String[] args) { Connection conn = null; try ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2006
    Posts
    60

    why I can't connect to Mysql using JDBC? Thank You!!!(SOLved)


    Can anyone give me a suggestion about this simple test code?
    ---------------------------------------------------------------------------------------------
    import java.sql.*;

    public class sql
    {
    public static void main (String[] args)
    {
    Connection conn = null;

    try
    {
    String userName = "root";
    String password = "root";
    String url = "jdbc:mysql://localhost/sysops/";
    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");
    e.printStackTrace();
    }
    finally
    {
    if (conn != null)
    {
    try
    {
    conn.close ();
    System.out.println ("Database connection terminated");
    }
    catch (Exception e) {
    e.printStackTrace();
    /* ignore close errors */ }
    }
    }
    }
    }
    -----------------------------------------------------------------------------------------------------------
    I use the stack trace, and get this err msg when I run it, google without achievement-_-
    -------------------------------------------------------------------------------------------------
    java.sql.SQLException: Incorrect database name 'sysops/'
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:2975)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:79
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java: 3700)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:12 03)
    at com.mysql.jdbc.Connection.createNewIO(Connection.j ava:256
    at com.mysql.jdbc.Connection.<init>(Connection.java:1 485)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:266)
    at java.sql.DriverManager.getConnection(DriverManager .java:525)
    at java.sql.DriverManager.getConnection(DriverManager .java:171)
    at sql.main(sql.java:15)
    ----------------------------------------------------------------------------------------------------------------

    I have a database named sysops in /var/lib/mysql/sysops
    ----------------------------------------------------------------------------------------------------------------
    drwx------ 2 mysql mysql 72 2007-02-15 20:32 bedrock
    -rw-rw---- 1 mysql mysql 10485760 2007-02-16 09:51 ibdata1
    -rw-rw---- 1 mysql mysql 5242880 2007-02-16 10:25 ib_logfile0
    -rw-rw---- 1 mysql mysql 5242880 2007-01-08 12:25 ib_logfile1
    drwx------ 2 mysql mysql 1752 2007-02-15 20:37 mysql
    -rw-rw---- 1 mysql mysql 3578 2007-02-16 10:25 mysqld.log
    -rw-rw---- 1 mysql mysql 5 2007-02-16 10:25 mysqld.pid
    srwxrwxrwx 1 mysql mysql 0 2007-02-16 10:25 mysql.sock
    drwx------ 2 mysql mysql 168 2007-02-15 20:58 sysops
    -----------------------------------------------------------------------------------------------------

    I don't know, Thanks for your kind help,
    Last edited by chrainy; 02-18-2007 at 12:52 PM. Reason: solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •