PDA

View Full Version : MySQL and JDBC


jhillman
05-06-2001, 09:40 PM
First off, does anyone know if HR will install a JDBC driver for a database system?

I would like to use my java application/applet to query my MySQL database system. From what I understand, there needs to be a JDBC driver installed to access the database.

Has anyone accomplished this task before? Is it even possible to do it here at HR? Any suggestions/commits/help would be very much appreciated.

Thank you,
-Joe
Got WebAppstogo?

eric418
05-07-2001, 03:27 PM
I didn't actually try it yet, but i think we can't use JDBC here.

I got a course work about JDBC too, i want to know the answer to this as well.

silverdog
05-07-2001, 05:24 PM
Can't you just upload the mysql jdbc drivers and add them into your classpath. You could also package them with your applet jar (just take the classes you need out of the mysql jdbc jar and add them to your applet jar.)
You can get the files from here http://www.mysql.com/downloads/api-jdbc.html

jhillman
05-07-2001, 06:09 PM
How is uploading a driver to any system going to accomplish anything without it being INSTALLED on the system. Actually HR would need to install the JDBC driver(s) on the system that contains the databases.

I have no problems added the sql classes that I want to use into my own jar file. Thats not the problem. The problem is having the SETUP done on the server side, which as we know, we don't have access to.

-Joe

eric418
05-07-2001, 07:39 PM
correct.

silverdog
05-07-2001, 09:25 PM
Why would you need the jdbc drivers installed on the server side? As long as you have the "database specific" jdbc drivers you can make the jdbc call to the database. You don't have to do anything special on the database side to accept JDBC that I am aware of.
ie.
This is how I do it on my personal machine with a local installation of mysql (I perform include any special setup - it's a vanilla mysql install)
With java you never have to "install" classes they only need to be compiled and able to be referenced. The only thing that ever needs to be "installed" is the jdk so you can run javac, java, etc...
/**
*
* A class for connecting to the MySQL database on silverdog.
*
*/
public class MyConnect
{

public Connection createConnection()
{
Connection connection = null;

try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();

//my connection statement
connection = DriverManager.getConnection("jdbc:mysql://localhost/database_name","userid","pword");

}
catch(SQLException sql)
{
System.out.println("SQLException: " + sql.getMessage());
System.out.println("SQLState : " + sql.getSQLState());
System.out.println("VendorError : " + sql.getErrorCode());
connection = null;
}
catch(Exception e)
{
System.out.println("Exception : "+ e.getMessage());
connection = null;
}
finally
{
return connection;
}

}

public ResultSet queryDB(String statement)
{
ResultSet rs = null;
Connection connection = null;
Statement stmt = null;
try
{
System.out.println("At querying DB");
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("About to get Connection");
//my connection statement
connection = DriverManager.getConnection("jdbc:mysql://localhost/databasename","userid","pword");
System.out.println("connection established");
stmt = connection.createStatement();
System.out.println("Statement Submitted");
rs = stmt.executeQuery(statement);
System.out.println("result set returned");

//Being good and closing everything

}
catch(SQLException sql)
{
System.out.println("SQLException: " + sql.getMessage());
System.out.println("SQLState : " + sql.getSQLState());
System.out.println("VendorError : " + sql.getErrorCode());
connection = null;
rs = null;
stmt=null;


}
catch(Exception e)
{
System.out.println("exception thrown in exception catch of MyConnect");
System.out.println("Exception : "+ e.getMessage());
e.printStackTrace();
connection = null;
rs = null;
stmt=null;
}
finally
{
try
{
//rs.close();
stmt.close();
connection.close();
}
catch(SQLException sql)
{
System.out.println("SQLException: " + sql.getMessage());
System.out.println("SQLState : " + sql.getSQLState());
System.out.println("VendorError : " + sql.getErrorCode());
connection = null;
rs = null;
stmt=null;


}
catch(Exception e)
{
System.out.println("Exception : "+ e.getMessage());
connection = null;
rs = null;
stmt=null;
}
return rs;
}
}
}

eric418
05-07-2001, 09:43 PM
thankyou, silverdog.

Let me try it out now.

jhillman
05-08-2001, 12:37 AM
Well, I've read the docs and tried everything you have listed above and it appears that the database can not be found. Basically I can not get a valid connection. ie. it's always null.

These are the steps I took:
1. Downloaded the mm.mysql.jdbc-1.0.zip file.
2. Copied all the classes from org\gjt\mm\mysql to my applet jar file (with my class files).

Here are the codes changes I made to your listing above:

In the createConnection function I changed the following line:
connection = DriverManager.getConnection("jdbc:mysql://localhost/my_database_name","userid","password"); Filling in the correct name for my database, userid and password:)

I implemeted the same changes to the queryDB(String statement) function.

I'm not quite sure how the 'Class.forName("org.gjt.mm.mysql.Driver").newInstance();' works?!?

I left the line as is and for all I know, this may be my problem, I don't know.

Am I taking the right approach here?

Thanks for all you input!

-Joe