Results 1 to 20 of 54
Thread: Mixing java and mysql
- 04-11-2012, 01:59 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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.
Error code: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()); } }
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.
- 04-11-2012, 03:42 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 04-11-2012, 03:56 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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
- 04-11-2012, 03:58 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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
Website: Learn Java by Examples
- 04-11-2012, 04:25 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
getting closer to figuring it out. I setup my CLASSPATH.
Now its saying that access is denied
You can see in my code from the first post I use my username and password within the code.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
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 passwordJava Code://Get a connection to the database connection = DriverManager.getConnection(connectionURL, "root", "daba19DE");
- 04-11-2012, 04:31 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 04-11-2012, 05:44 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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
- 04-11-2012, 05:48 AM #8
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 04-11-2012, 06:18 AM #9
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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
- 04-11-2012, 06:23 AM #10
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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?
Website: Learn Java by Examples
- 04-11-2012, 06:30 AM #11
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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"); } }
- 04-11-2012, 07:02 AM #12
Re: Mixing java and mysql
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-11-2012, 07:57 AM #13
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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?
Website: Learn Java by Examples
- 04-11-2012, 10:44 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 04-11-2012, 10:56 PM #15
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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()); } }
- 04-11-2012, 11:02 PM #16
- 04-11-2012, 11:05 PM #17
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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.
- 04-12-2012, 02:02 AM #18
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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...
As you can see i have downloaded mysql-connector-java-5.1.18-bin.jar using yum install commandJava 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
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.
- 04-12-2012, 09:39 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 04-12-2012, 06:06 PM #20
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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:
Here is the result when i type ls:Java Code:[root@engagelinux ~]# whereis java java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java
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?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
Last edited by bad biddy; 04-12-2012 at 06:38 PM.
Similar Threads
-
Is this a java issue? JApplet with MySQL access.. can't connect to mySQL
By cplredhartsock in forum Java AppletsReplies: 4Last Post: 03-04-2012, 11:26 PM -
Does latest JDK fully support mixing of heavy and light components
By hello2010 in forum AWT / SwingReplies: 7Last Post: 03-18-2011, 05:28 AM -
Mixing 2 audio stream threads for java mobile
By Severus in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 08-21-2010, 06:16 PM -
AWT/Swing Mixing Still and issue on MAC OSX?
By Confused in forum AWT / SwingReplies: 2Last Post: 02-21-2010, 08:34 PM -
Heavy and light component mixing
By Bojevnik in forum AWT / SwingReplies: 2Last Post: 08-13-2007, 11:23 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks