1752815 Members
5727 Online
108789 Solutions
New Discussion юеВ

Java

 
SOLVED
Go to solution
Piotr Kirklewski
Super Advisor

Java

Hi there
I'm trying to connect to mysql from java programm.
I'm not using any IDE, just vi.
jdk is installed as I'm able to create some simple programs.
I need to know how can I load the necesery libraries to be able to run following program:

package com.stardeveloper.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcExample2 {

public static void main(String args[]) {
Connection con = null;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///test",
"root", "secret");

if(!con.isClosed())
System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");

} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}





Jesus is the King
3 REPLIES 3
Steven E. Protter
Exalted Contributor
Solution

Re: Java

Shalom,

It might be your server version of Java.

swlist -l product | grep jre

or jdk

http://www.hp.com/go/java

Perhaps download and install a new release on your server.

IT could be bad code.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Piotr Kirklewski
Super Advisor

Re: Java

I've installed jdbc in my system.
Now I'm trying test it with some simple program but it wont work:

javac Jdbc11.java


Jdbc11.java:5: cannot find symbol
symbol : class Statement
location: class Jdbc11
Statement stmt;

Anyone ?

Jesus is the King
Ben Dehner
Trusted Contributor

Re: Java

You need to set up the execution environment to find the JDBC libraries. First, this means setting the classpath variable to include the location of the JDBC jar files, either through the command line or environment setting:

java -classpath /some/path/to/library.jar ...

or

export CLASSPATH=/some/path/to/library.jar
java ...

Second, it depends on what type of JDBC driver this is; I haven't worked with MySQL, I don't know. If this is a type 3 (pure Java) library, then the above should work. However, if it is a type 2 (Java pass-through), then there is a thick client layer behind the JDBC. This usually requires a shared library that the Java envinmont can load, which in turn means setting the SHLIB_PATH environment variable to the location of the client libraries:

export SHLIB_PATH=/path/to/mysql/lib
export CLASSPATH=/path/to/mysql/jarfiles.jar
java ...

If you are getting a specific run-time exception, this should indicate where the problem is. A ClassNotFoundException indicates that the JVM can't find the specific JDBC implementation you need. An UnsatisfiedLinkError will indicate that the JVM can't find the shared libraries for a type 2 driver.
Trust me, I know what I'm doing