Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java and mySQL 1

Status
Not open for further replies.

perlone

Programmer
May 20, 2001
438
US
I just want to know that is there anyway I could connect java with my mySQL database? Thanks. There is no Knowledge that is not power.
Age: 16
E-mail: projectnet01@yahoo.com
Company:(not done yet) :)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
Yes my friend there is!
You can download the JDBC driver from:

Documentation is here:

Here is a little code sample to test that you have loaded drivers and can make a connection...

Obviously the database info is not correct but you get the idea

<-Begin code here->

import java.sql.*;

// Notice, do not import org.gjt.mm.mysql.* or you will have problems!

public class LoadDriver {

public static void main(String[] Args) {

// The newInstance() call is a work around for some broken
// Java implementations

try {
Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;).newInstance();
}

catch (Exception E) {
System.err.println(&quot;Unable to load driver.&quot;);
E.printStackTrace();
}

try {

String MYSQL_HOST = &quot;richese.csci.unt.edu&quot;;
String MYSQL_DB = &quot;testdb&quot;;
String MYSQL_USER = &quot;test&quot;;
String MYSQL_PASSWD = &quot;testpw&quot;;
String MYSQL_TABLE = &quot;pets&quot;;


// Replace
// _server_ with the remote server name
// _db_ with the remote database name
// _user_ with the database user name
// _pwd_ with the user password

Connection Conn = DriverManager.getConnection(
&quot;jdbc:mysql://&quot; + MYSQL_HOST + &quot;/&quot; + MYSQL_DB +
&quot;?user=&quot; + MYSQL_USER + &quot;&password=&quot; + MYSQL_PASSWD);

Statement Stmt = Conn.createStatement();

// Replace
// _table_ with the remote database table name

ResultSet RS = Stmt.executeQuery(&quot;SELECT * from pets&quot;);

System.out.println(&quot;Pet Name Type&quot;);
System.out.println(&quot;--------- ----&quot;);

String buffer = &quot;&quot;;

while (RS.next()) {
buffer = RS.getString(1);
int i = buffer.length();
if (i < 9) {
buffer = &quot; &quot;;
System.out.println(RS.getString(1) + buffer.substring(1,9-i) +
&quot; &quot; + RS.getString(2));
} else {
System.out.println(RS.getString(1) + &quot; &quot; + RS.getString(2));
}
}

// Clean up after ourselves
RS.close();
Stmt.close();
Conn.close();
}

catch (SQLException E) {
System.out.println(&quot;SQLException: &quot; + E.getMessage());
System.out.println(&quot;SQLState: &quot; + E.getSQLState());
System.out.println(&quot;VendorError: &quot; + E.getErrorCode());
}

}

}
 
Thank You! There is no Knowledge that is not power.
Age: 16
E-mail: projectnet01@yahoo.com
Company:(not done yet) :)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
My pleasure. I appreciate the fact that you would use that particular mix of tools to build your project. I am a huge fan of mySQL and Java.
Let me know if I can be of assistance.
 
Thanks :). Knows C++, PERL, PHP. Trying to learn Java :). Thanks again. There is no Knowledge that is not power.
Age: 16
E-mail: projectnet01@yahoo.com
Company:(not done yet) :)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
I am going to develop a software for my special problem for under graduate corriculum using Java and with mySQL as the data storage. can you help me get related topics or similar project. thank you
 
Can some-one please tell me what would be a good piece of software to write to have on one`s CV that would impress?
please conact me at kaneiv@hotmail.com
thanx
KaneIV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top