LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gentoo +jdbc errors with sun examples? (https://www.linuxquestions.org/questions/programming-9/gentoo-jdbc-errors-with-sun-examples-449684/)

trscookie 05-29-2006 04:23 PM

gentoo +jdbc errors with sun examples?
 
hello all im trying to get this bloomin jdbc example from the sun website working but all i get is a load of errors, do you know what could be causing this and how to solve it? please also i am on a very tight schedule cheers.

the sun example:
Code:

package JDBC.IncertionClass;

import java.sql.*;
import java.io.*;


public class IncertionClass {

        public static void main(String[] args)  throws ClassNotFoundException,
                                                      IOException {
        try {
        /**
        *  Load jdbc driver for MySQL
        */
          Class.forName( "com.mysql.jdbc.Driver");
           
      //  Loads database name of a mysql database into url
      //localhost says that it is on the current machine and test says that the
      //  name of the database is test.
     
            String url = "jdbc:mysql://localhost/test";
     
      //  We have to establish a jdbc connection to the database

            Connection connection = DriverManager.getConnection(url);
       
            /* The insert statement will insert the record into the defined
            * table.
            */ 
            int sid=126;
            String nameStudent ="Tom";
            String courseName ="cs 223";
            String letterGrade="D";
            String qs = "insert into students values ("+sid+","+"'"+nameStudent
                                +"','"+courseName+"','"+letterGrade+"')";
       
            Statement stmt = connection.createStatement();
            stmt.executeUpdate(qs);
            System.out.println("Insertion is complete");

            stmt.close();
            connection.close();
               
        } catch (SQLException sqle){
                        sqle.printStackTrace();
                       
                        while (sqle != null) {
                                String logMessage = "\n SQL Error: "+
                                  sqle.getMessage() + "\n\t\t"+
                                  "Error code: "+sqle.getErrorCode() +
                                              "\n\t\t"+
                                  "SQLState: "+sqle.getSQLState()+"\n";
                        System.out.println(logMessage);
                        sqle = sqle.getNextException();
                        }
                } 
        }
}

my error:
Code:

SQL Error: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:283)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:2541)
        at com.mysql.jdbc.Connection.<init>(Connection.java:1474)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:264)
        at java.sql.DriverManager.getConnection(DriverManager.java:512)
        at java.sql.DriverManager.getConnection(DriverManager.java:193)
        at JDBC.IncertionClass.IncertionClass.main(IncertionClass.java:25)


** END NESTED EXCEPTION **



Last packet sent to the server was 25 ms ago.
                Error code: 0
                SQLState: 08S01

ive tried runnign as root also no success.

zeitounator 05-29-2006 04:46 PM

-------
Updated
-------
I left the original message below just for memory. In fact, after checking the error code corresponding to the sqlstate, it comes out its a socket error. I just came accross the following page in the mysql doc:

http://dev.mysql.com/doc/refman/5.0/en/cj-faq.html

From what you will read there, jdbc driver for mysql needs to connect over the network and cannot use a socket connection. So you need to make sure network is enabled for your mysql server and you should connect to 127.0.0.1 (or localhost.localdomain) instead of localhost. You will probably have to add some grants as well. All is explained on the above link. Good luck

----
Original message
----
Well, the exception message pretty much says it all: "connection refused".
1- check that the mysql server is running
2- if it is, check it is accepting connection on a socket (and not only on network)
3- if it is accepting socket connections, see if it accepts anonymous connections on the "test" database.
4- if it does not accept anonymous connections, add a login/pass to the line:
Connection connection = DriverManager.getConnection(url, "login", "password");

Good luck

trscookie 05-29-2006 05:07 PM

cheers, nice signature btw!!

mrcheeks 05-29-2006 05:54 PM

do you have the mysql jar in your classpath?

trscookie 05-30-2006 03:47 AM

yea you have to emerge jdbc-mysql otherwise it says invalid driver path or something, cheers trscookie.


All times are GMT -5. The time now is 02:55 AM.