Results 21 to 40 of 54
Thread: Mixing java and mysql
- 04-13-2012, 09:10 AM #21
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
- 04-13-2012, 09:38 AM #22
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Mixing java and mysql
A normal java project is structured:
That's a basic structure. As it says, classes is optional and would involve using the -d option of javac. Without that then your class files will be placed beside the source files in the src folder (which may be fine for your needs).Java Code:<My-Project-Directory> -- src -- classes (though you may simpyl have the classes in the src depending on how you compile) -- lib (this is where you dependent jar files go)
That lib directory is the one I'm talking about.
Now, assuming you're in the <My-Project-Directory> directory you would do this to compile:
javac -cp ./src;./lib/*.jar <classes to compile eg src/somepackage/yourclass.java>
To run:
java -cp ./src;./lib/*.jar <your main class eg somepackage.yourclass>Last edited by Tolls; 04-13-2012 at 09:39 AM. Reason: Correct last command
Please do not ask for code as refusal often offends.
- 04-13-2012, 09:52 AM #23
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Mixing java and mysql
Hi, you'd better take a little bit time to read about the classpath in Java, here is a tutorial on it PATH and CLASSPATH (The Java™ Tutorials > Essential Classes > The Platform Environment).
Basically if you work from a terminal just use the -cp option to define the classpath when you are executing your program using the java command. That was Toll tried to tell you on the previous answer. Just place the mysql.jar in the same location with your class.
For example:
java -cp .:mysql-connector-java-5.1.18-bin.jar ProgramOne
The dot symbol (.) is to add the current directory into the path so it can find your ProgramOne class.Website: Learn Java by Examples
- 04-13-2012, 12:10 PM #24
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Mixing java and mysql
And that post reminded me, you'll probably have to check whether it's a ';' or a ':' for the separator inbetween different paths.
I can never remember.Please do not ask for code as refusal often offends.
- 04-13-2012, 12:14 PM #25
- 04-13-2012, 06:47 PM #26
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
Java Code:[root@engagelinux ~]# java -cp .:mysql-connector-java-5.1.18-bin.jar ProgramOne e.getMessage():You have an error in your SQL syntax; check the manual that corre sponds 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 t hat corresponds to your MySQL server version for the right syntax to use near '? ???????????????' at line 1 e.getStackTrace():[Ljava.lang.StackTraceElement;@1d8c3835
[root@engagelinux ~]# java -cp .:mysql-connector-java-5.1.18-bin.jar ProgramOne
e.getMessage():You have an error in your SQL syntax; check the manual that corre sponds 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 t hat corresponds to your MySQL server version for the right syntax to use near '? ???????????????' at line 1
e.getStackTrace():[Ljava.lang.StackTraceElement;@1d8c3835
I repasted the error because it says it gets cut off when using the code tags.
- 04-13-2012, 07:03 PM #27
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
I can compile and run local java files just fine.
When I compile the class file is created within the same directory. That is how I will keep it.
When I run java -version I receive
So the path must be setup correctly. Right?Java Code:[root@engagelinux ~]# java -version java version "1.5.0" gij (GNU libgcj) version 4.4.6 20110731 (Red Hat 4.4.6-3) Copyright (C) 2007 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- 04-13-2012, 07:40 PM #28
Re: Mixing java and mysql
[QUOTE=bad biddy;278889]
Another name for GNU Java aka gcj is non-Java.Java Code:gij (GNU libgcj) version 4.4.6 20110731 (Red Hat 4.4.6-3)
Get rid of it and install an Oracle JDK.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-13-2012, 09:29 PM #29
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
I installed oracle jdk
here is the java -version command now
Now that we have a new java installed. I compiled it and tried to run the programJava Code:[root@engagelinux ~]# java -version java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.6) (rhel-1.43.1.10.6.el6_2-x86_64) OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)
Here is the error code. Like before its not seeing the jdbc driver. I just have to setup the CLASSPATH and hopefully that fixes the driver issue.
With a Classpath you are pointing it to a file or location for the java driver correct? Do I want to point it to use/lib/java or to another place?Java Code:[root@engagelinux ~]# java ProgramOne e.getMessage():com.mysql.jdbc.Driver e.getLocalizedMessage():com.mysql.jdbc.Driver e.getStackTrace():[Ljava.lang.StackTraceElement;@5d888759
Java Code:[root@engagelinux ~]# whereis java java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java /usr/share/man/man1/java.1.gz
- 04-13-2012, 10:12 PM #30
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
I tried following this guide for setting class path but I'm still getting jdbc error
How to Set Java Home on CentOS | eHow.com
- 04-16-2012, 02:04 AM #31
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
Still haven't figured out setting the class path...
- 04-16-2012, 10:42 AM #32
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Mixing java and mysql
The classpath is fine.
That error is nothing to do with the classpath.
Exactly what it is to do with requires you to priont the stack trace, as I requested waaaaay back up the thread!Please do not ask for code as refusal often offends.
- 04-16-2012, 04:15 PM #33
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
I just added the print stack trace below those other error messages-
like this:Java Code:[root@engagelinux ~]# java ProgramOne e.getMessage():com.mysql.jdbc.Driver e.getLocalizedMessage():com.mysql.jdbc.Driver e.getStackTrace():[Ljava.lang.StackTraceElement;@2e6e1408 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:186) at ProgramOne.main(ProgramOne.java:20)
Java Code:catch(Exception e) { System.out.println("e.getMessage():"+e.getMessage()); System.out.println("e.getLocalizedMessage():"+e.getLocalizedMessage()); System.out.println("e.getStackTrace():"+e.getStackTrace()); e.printStackTrace();
- 04-16-2012, 04:26 PM #34
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Mixing java and mysql
Post #26 was not a ClassNotFoundException.
Launch your code with whatever command you used in that post, and then post the full stack trace.Please do not ask for code as refusal often offends.
- 04-16-2012, 04:41 PM #35
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
I don't get what you mean.... that is how I run the program:
I just: java ProgramOne to run it. Those are the errors that popped up.
How do i show the full stack trace?
- 04-16-2012, 05:13 PM #36
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Mixing java and mysql
Please do not ask for code as refusal often offends.
- 04-16-2012, 05:57 PM #37
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
It works!!!!
I just gotta edit the code now for the project.Java Code:[root@engagelinux ~]# java -cp .:mysql-connector-java-5.1.18-bin.jar ProgramOne PRODUCT ID PRODUCT NAME SUPPLIER ID CATEGORY ID UNIT PRICE 1 Flash Disk 1000 2000 10.0 2 SSD Disk 1000 2000 100.0 3 SD Card 1000 2000 15.0 4 USB Drive 1000 2000 29.0 5 Micro SD Drive 1000 2000 10.0 6 Head Phone 1000 2001 9.99
- 04-16-2012, 05:58 PM #38
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
I need to figure out my spacing now.. as you can tell its off by quite a bit
- 04-16-2012, 09:50 PM #39
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Mixing java and mysql
Nevermind the spacing thats how its suppose to be looking at our example...
Part two of the assignment is basically to create a program two that will add additional products to the MySQL database.
Here is the code for it.
Java Code:[root@engagelinux ~]# cat ProgramTwo.java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class ProgramTwo { public static void main(String[]args){ //Establish connection to MYSQL database String connectionURL = "jdbc:mysql://localhost/warehouse"; Connection connection = null; Statement statement = null; String sql = "insert into products (productName, supplierID, categoryID, unitPrice,) values ('Hard Disk','5555','6666','59.99')"; String username = "bdewall"; String password = "daba19DE"; try{ Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, username, password); statement = connection.createStatement(); int count = statement.executeUpdate(sql); System.out.println(count + "rows updated."); }catch (ClassNotFoundException e){ System.out.println("Unable to load MySql jdbc drivers."); e.printStackTrace(); }catch(SQLException e) { System.out.println("Unable to establish connection with the database."); e.printStackTrace(); }finally{ if(connection !=null){ try{ connection.close(); }catch(SQLException e){ System.out.println("Exception occured while closing database connection."); e.printStackTrace(); } } } } }
Its telling me that its unable to establish a connection to the database.
Java Code:[root@engagelinux ~]# java -cp .:mysql-connector-java-5.1.18-bin.jar ProgramTwo Unable to establish connection with the database. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ') values ('Hard Disk','5555','6666','59.99')' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1749) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1666) at ProgramTwo.main(ProgramTwo.java:21)Last edited by bad biddy; 04-16-2012 at 09:53 PM.
- 04-17-2012, 09:36 AM #40
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Mixing java and mysql
No it's not.
You are telling you it's unable to connect to the database.
Look at your catch block.
Any SQLException cause your code to claim it has failed to connect.Java Code:catch(SQLException e) { System.out.println("Unable to establish connection with the database."); e.printStackTrace(); }
Just stick to printing the stack trace. It has all the details you need for your exception.
In this case it's your SQL that's wrong.Please do not ask for code as refusal often offends.
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