Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

MySql help Request

Hi everyone,

I need some help using java and mysql. Has anyone heard and used MySql??

I have downloaded and installed the MySql server and created a small database file called t1 (just as a test file). It has one table in it. I have also downloaded a file from the MySql website call a MySql connector for Java. I extracted the files in the MySql directory. But I don't know what to do next. There is a file called MySql-connector-java-3.1.10-bin.jar.

How do I use the MySql server and it databases in a Java program? I have used a Microsoft Access database with a java program. I remember having to go to the windows ODBC Data Source Administrator and add a User DSN for the database file that I wanted to use. I tried doing that but unsuccessfully. MySql wasn't in the list to choose from.

I really need help here. I posted messages at the MySql site but no one has replied.

Richard
[939 byte] By [airrazor] at [2007-11-11 6:53:06]
# 1 Re: MySql help Request
Can't really help you with Java, but did you happen to take a look at the documentation?

http://dev.mysql.com/doc/mysql/en/java-connector.html
pclement at 2007-11-11 23:48:03 >
# 2 Re: MySql help Request
Thanks for your reply but do you know where to set tht CLASSPATH so that I can have access to the driver from any directory.
airrazor at 2007-11-11 23:49:08 >
# 3 Re: MySql help Request
copy that .jar file in to ur jre and paste it inside

/jre<version>/lib/ext/

after this u can access the database using jdbc
vsorc at 2007-11-11 23:50:07 >
# 4 Re: MySql help Request
Thanks for your help but i am still doing something wrong here. I did what you said to do but it still doesn't work. I get a runtime error SQL Error: java.sql.SQLException: No suitable driver 0 08001

Here is the code I am running:

import java.sql.*;

public class mysqldatabasetest1 {
public static void main(String[] arguments) {
String data = "jdbc:mysql:t1";

try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
data, "", "");
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery(
"SELECT * " +
"FROM male ");
while(rec.next()) {
int n = 1;
System.out.println(rec.getString(++n) + "\t"
+ rec.getString(++n) + "\t"
+ rec.getString(++n));
}

st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println(" Error: " + e.toString()
+ e.getMessage());
}
}
}

Something is still wrong. Please go through the code.
Thanks
airrazor at 2007-11-11 23:51:01 >
# 5 Re: MySql help Request
String data = "jdbc:mysql://<ip address >:<port no>t1";

ip address-the ip address of the machine where u hve ur database if u are in a network

port no-the port no in which mysql is running--if u hvnt assigned any port no
the default no will be-3306

if u r not in a network

the try using

String data = "jdbc:mysql://localhost:3306/t1";

---------------

now the second thing is

Connection conn = DriverManager.getConnection(
data, "<your my sql userid>", "<password>");

if u r loging in the database as administrator

then

Connection conn = DriverManager.getConnection(
data, "root", "<password>");

try this ....sometimes this will solve the problem

---------------

vsorc-:)
vsorc at 2007-11-11 23:52:08 >
# 6 Re: MySql help Request
I un-installed all of my java components, deleted all of the java directories and then downloaded and installed the latest jdk 5. Then I followed what vsorc suggested to do and it worked. Thanks vsorc for your help. I am using a gui windows application to write my java code (JCreator) and it is working fine now.

Now I just have to be able to make it work from the command line. I am having troubles I think I need to set the path to the java and javac executables because I can't use them from the current directory where the program resides. How do you do this? I thought I knew how but I guess not.
airrazor at 2007-11-11 23:53:12 >