Page 1 of 3 123 LastLast
Results 1 to 20 of 54
Like Tree1Likes

Thread: Mixing java and mysql

  1. #1
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Mixing java and mysql

    I can get the program to compile but I keep getting a error.

    Here is my code. I'll post the error code below.

    Java Code:
    [root@engagelinux ~]# cat ProgramOne.java
            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.ResultSet;
            import java.sql.SQLException;
            import java.sql.Statement;
    
            public class ProgramOne {
    
                    public static void main(String[]args){
    
                    //Establish connection to MySQL database
                    String connectionURL = "root:mysql://localhost/warehouse";
    
                    Connection connection = null;
                    ResultSet resultSet = null;
                    StringBuffer sb = new StringBuffer();
                    try {
    
                    //Load the database driver
                    Class.forName("com.mysql.root.Driver");
    
                    //Get a connection to the database
                    connection = DriverManager.getConnection(connectionURL, "root", "daba19DE");
    
                    //Select the data from the database
                    String sql = "select * from products;";
                    Statement statement = connection.createStatement();
                    statement.executeQuery (sql);
                    resultSet = statement.getResultSet();
    
                    System.out.println("PRODUCT ID" + "\t" + "PRODUCT NAME" + "\t" + "SUPPLIER ID" + "\t" + "CATEGORY ID" + "\t" + "UNIT PRICE");
                    while(resultSet.next()){
                            sb.append(resultSet.getInt("productID") + "\t");
                            sb.append(resultSet.getString("productName") + "\t");
                            sb.append(resultSet.getInt("supplierID") + "\t");
                            sb.append(resultSet.getInt("categoryID") + "\t");
                            sb.append(resultSet.getFloat("unitPrice") + "\t");
                            sb.append("\n");
                    }
                    }
    
                    catch(Exception e) {
                            System.out.println("e.getMessage():"+e.getMessage());
                            System.out.println("e.getLocalizedMessage():"+e.getLocalizedMessage());
                            System.out.println("e.getStackTrace():"+e.getStackTrace());
                    }
    
                    finally{
                            if(resultSet != null){
                    try {
    
                            connection.close();
                    } catch(SQLException e) {
                            System.out.println("Exception occured while closing database connection.");
                            e.printStackTrace();
                            }
                    }
                    }
                    System.out.println(sb.toString());
                    }
                    }
    Error code:

    Java Code:
    [root@engagelinux ~]# java ProgramOne
    e.getMessage():com.mysql.root.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
    e.getLocalizedMessage():com.mysql.root.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
    e.getStackTrace():[Ljava.lang.StackTraceElement;@5c80aefa

    Can you help me figure this out.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Mixing java and mysql

    The JDBC url for MySQL database is jdbc:mysql://localhost/warehouse and the JDBC driver class is com.mysql.jdbc.Driver.

  3. #3
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    Thanks for the quick reply.

    I changed it to jdbc and I'm getting this error when trying to run it:

    Java Code:
    [root@engagelinux ~]# java ProgramOne
    e.getMessage():com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
    e.getLocalizedMessage():com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
    e.getStackTrace():[Ljava.lang.StackTraceElement;@40518ddb

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Mixing java and mysql

    Make sure you have the MySQL JDBC driver and it is available in your application classpath. If you don't have it you can download it here: MySQL :: Download Connector/J

  5. #5
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    getting closer to figuring it out. I setup my CLASSPATH.

    Now its saying that access is denied


    Java Code:
    [root@engagelinux ~]# java ProgramOne
    e.getMessage():Access denied for user 'root'@'localhost' (using password: YES)
    e.getLocalizedMessage():Access denied for user 'root'@'localhost' (using password: YES)
    e.getStackTrace():[Ljava.lang.StackTraceElement;@54b5ac36
    You can see in my code from the first post I use my username and password within the code.

    Java Code:
       //Get a connection to the database
                    connection = DriverManager.getConnection(connectionURL, "root", "daba19DE");
    This is on a school network that is why its requiring a username and password I would assume. At least from the example given the teacher uses his username and password

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Mixing java and mysql

    Make sure that the username and password required to connect to the database is valid. If you said that the database is in the school network then you have to know the IP address of the database server instead of using localhost.

  7. #7
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    I asked someone from class about it and he said that mysql won't let root do a remote connection to the database so i had to create a MySQL username and password.

    So i created a account and password and now it gives me this error

    Java Code:
    [root@engagelinux ~]# java ProgramOne
    e.getMessage():com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
    e.getLocalizedMessage():com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
    e.getStackTrace():[Ljava.lang.StackTraceElement;@63747f6b

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Mixing java and mysql

    I think you've lost your classpath configuration again that's why it didn't find the JDBC driver again. It clearly said that it couldn't find the com.mysql.jdbc.Driver class.

  9. #9
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    I reestablished the class path and boom what do ya know another error this one is crazy....


    Java Code:
    [root@engagelinux ~]# java ProgramOne
    e.getMessage():You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
    e.getLocalizedMessage():You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
    e.getStackTrace():[Ljava.lang.StackTraceElement;@503eb8d2

  10. #10
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Mixing java and mysql

    That means you have an error in your SQL query. Can you post what query you are trying to execute?

  11. #11
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    Isn't this the SQL query?

    Java Code:
       //Select the data from the database
                    String sql = "select * from products;";
                    Statement statement = connection.createStatement();
                    statement.executeQuery (sql);
                    resultSet = statement.getResultSet();
     
                    System.out.println("PRODUCT ID" + "\t" + "PRODUCT NAME" + "\t" + "SUPPLIER ID" + "\t" + "CATEGORY ID" + "\t" + "UNIT PRICE");
                    while(resultSet.next()){
                            sb.append(resultSet.getInt("productID") + "\t");
                            sb.append(resultSet.getString("productName") + "\t");
                            sb.append(resultSet.getInt("supplierID") + "\t");
                            sb.append(resultSet.getInt("categoryID") + "\t");
                            sb.append(resultSet.getFloat("unitPrice") + "\t");
                            sb.append("\n");
                    }
                    }

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Mixing java and mysql

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Mixing java and mysql

    I didn't see any error with the select * from products; query. Are you sure this is the query you were executing?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Mixing java and mysql

    Can you do printStackTrace() instead of faffing with those three logging statements?
    You're losing your stack trace, which might explain what's happening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    I don't know how to do a printStackTrace()

    This is all the code I have for the program besides the database I created in MySQL.

    Java Code:
    [root@engagelinux ~]# cat ProgramOne.java
            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.ResultSet;
            import java.sql.SQLException;
            import java.sql.Statement;
     
            public class ProgramOne {
     
                    public static void main(String[]args){
     
                    //Establish connection to MySQL database
                    String connectionURL = "root:mysql://localhost/warehouse";
     
                    Connection connection = null;
                    ResultSet resultSet = null;
                    StringBuffer sb = new StringBuffer();
                    try {
     
                    //Load the database driver
                    Class.forName("com.mysql.root.Driver");
     
                    //Get a connection to the database
                    connection = DriverManager.getConnection(connectionURL, "root", "daba19DE");
     
                    //Select the data from the database
                    String sql = "select * from products;";
                    Statement statement = connection.createStatement();
                    statement.executeQuery (sql);
                    resultSet = statement.getResultSet();
     
                    System.out.println("PRODUCT ID" + "\t" + "PRODUCT NAME" + "\t" + "SUPPLIER ID" + "\t" + "CATEGORY ID" + "\t" + "UNIT PRICE");
                    while(resultSet.next()){
                            sb.append(resultSet.getInt("productID") + "\t");
                            sb.append(resultSet.getString("productName") + "\t");
                            sb.append(resultSet.getInt("supplierID") + "\t");
                            sb.append(resultSet.getInt("categoryID") + "\t");
                            sb.append(resultSet.getFloat("unitPrice") + "\t");
                            sb.append("\n");
                    }
                    }
     
                    catch(Exception e) {
                            System.out.println("e.getMessage():"+e.getMessage());
                            System.out.println("e.getLocalizedMessage():"+e.getLocalizedMessage());
                            System.out.println("e.getStackTrace():"+e.getStackTrace());
                    }
     
                    finally{
                            if(resultSet != null){
                    try {
     
                            connection.close();
                    } catch(SQLException e) {
                            System.out.println("Exception occured while closing database connection.");
                            e.printStackTrace();
                            }
                    }
                    }
                    System.out.println(sb.toString());
                    }
                    }

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Mixing java and mysql

    Quote Originally Posted by bad biddy View Post
    I don't know how to do a printStackTrace()
    Java Code:
                    } catch(SQLException e) {
                            System.out.println("Exception occured while closing database connection.");
                            e.printStackTrace(); // <---- what's that?
                            }
                    }
    Huh?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    Lol umm.... I guess I do.... I basically copied the entire code from a example the professor has given me.

  18. #18
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    Ok so I think the problem is with the classpath not linking the JDBC driver

    So let me start over...

    Java Code:
    [root@engagelinux ~]# ls
    Desktop           Downloads           Music                                nmap-5.51.tgz    PrintClassPath.java  ProgramTwo.class  Templates
    DetermineOs.java  GetSystemTime.java  mysql-connector-java-5.1.18-bin.jar  nmap-5.51.tgz.1  ProgramOne.class     ProgramTwo.java   Videos
    Documents         java3con.sp         nmap-5.51                            Pictures         ProgramOne.java      Public
    As you can see i have downloaded mysql-connector-java-5.1.18-bin.jar using yum install command

    I have tried setting the CLASSPATH to it but maybe I'm doing it wrong.... How would you guys set the class path in my instance??

    This file is located in my root.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Mixing java and mysql

    So what is the full exception you are now getting when you use printStackTrace()?

    Also, how are you running this?

    Finally, I would stick all my dependencies inside a lib folder and add that folder to my runtime classpath.
    If I was running from the command line that would be:
    java -cp <path to jars>/*.jar;. <my main class>
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Mixing java and mysql

    Ok Tolls you might have to walk me through this.. Im just starting java.

    Basically at college we have all been given a private/ virtual machine to work with. We connect to that machine via ssh through putty.

    BTW Im running a 64 bit CentOS

    So your saying I should move my .jar file to a folder inside the lib folder?

    When i type whereis java I get this result:

    Java Code:
    [root@engagelinux ~]# whereis java
    java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java
    Here is the result when i type ls:

    Java Code:
    [root@engagelinux ~]# ls
    Desktop             mysql-connector-java-5.1.18-bin.jar  ProgramOne.java
    DetermineOs.java    nmap-5.51                            ProgramTwo.class
    Documents           nmap-5.51.tgz                        ProgramTwo.java
    Downloads           nmap-5.51.tgz.1                      Public
    GetSystemTime.java  Pictures                             Templates
    java3con.sp         PrintClassPath.java                  Videos
    Music               ProgramOne.class
    So I should move mysql-connector-java-5.1.18-bin.jar to a folder within in lib or move it to the java folder within lib?
    Last edited by bad biddy; 04-12-2012 at 06:38 PM.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 03-04-2012, 11:26 PM
  2. Replies: 7
    Last Post: 03-18-2011, 05:28 AM
  3. Mixing 2 audio stream threads for java mobile
    By Severus in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-21-2010, 06:16 PM
  4. AWT/Swing Mixing Still and issue on MAC OSX?
    By Confused in forum AWT / Swing
    Replies: 2
    Last Post: 02-21-2010, 08:34 PM
  5. Heavy and light component mixing
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 11:23 AM

Posting Permissions

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