Bluehost.com Web Hosting $6.95

Connecting to mysql via Java

This is a discussion on Connecting to mysql via Java within the MySQL Database forums, part of the Database Forums category; Hi, I am new to mysql. I need to connect to a mysql server from a Java application. Can someone ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-04-2006
yankeerivera@yahoo.com
 
Posts: n/a
Default Connecting to mysql via Java

Hi, I am new to mysql. I need to connect to a mysql server from a Java
application. Can someone share some sample code or point me to the
right place for a sample?

I am assuming that I'll have to download a Mysql driver and I'll be
able to use the usual java.sql.* classes.

Reply With Quote
  #2 (permalink)  
Old 12-04-2006
David Harper
 
Posts: n/a
Default Re: Connecting to mysql via Java

yankeerivera@yahoo.com wrote:
> Hi, I am new to mysql. I need to connect to a mysql server from a Java
> application. Can someone share some sample code or point me to the
> right place for a sample?
>
> I am assuming that I'll have to download a Mysql driver and I'll be
> able to use the usual java.sql.* classes.


You might find it helpful to read a couple of books:

"Learning Java"

http://www.oreilly.com/catalog/learnjava2/

"Database Programming with JDBC & Java"

http://www.oreilly.com/catalog/jdbc2/index.html

The first book contains plenty of examples of Java techniques, such as
opening a file and reading it a line at a time.

The second book focusses on database programming with Java.

You should then read the documentation about Connector/J, the JDBC
library for MySQL:

http://dev.mysql.com/doc/refman/4.1/en/connector-j.html

This provides advice about how to obtain and install the latest version
of Connector/J, and examples showing how to connect to MySQL and send
commands and queries to the MySQL server.

In a recent thread on this subject, Jeff Summers also recommended "MySQL
and Java Developers Guide" by Matthews, Cole, and Gradecki, from Wiley.
Mark Matthews is the principal developer of Connector/J, so you'll be
learning from the expert.

Good luck with your project.

David Harper
Cambridge, England
Reply With Quote
  #3 (permalink)  
Old 12-04-2006
Dr.Zoidberb
 
Posts: n/a
Default Re: Connecting to mysql via Java

yankeerivera@yahoo.com wrote:
> Hi, I am new to mysql. I need to connect to a mysql server from a Java
> application. Can someone share some sample code or point me to the
> right place for a sample?
>
> I am assuming that I'll have to download a Mysql driver and I'll be
> able to use the usual java.sql.* classes.


That is correct you will need the Java Connector found at
http://dev.mysql.com/downloads

The basic flow for talking to MySQL via java is:

1. Load the Driver
2. Make the Connection
3. Make the query
4. Do something with results

Sample/Starter code:

public static boolean load_mysql_driver()
{
try {
// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.jdbc.Driver").newInstance ();
} catch (Exception ex) {
System.err.println(ex);// handle the error
return false;
}
return true;
}

public static Connection connect_mysql(String hostname, String
database, String username, String password)
{
Connection conn;
try {

conn = DriverManager.getConnection("jdbc:mysql://" + hostname +
"/"+ database +"?user="+username+"&password=" + password);

} catch (SQLException ex) {
// handle any errors
System.err.println("SQLException: " + ex.getMessage());
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("VendorError: " + ex.getErrorCode());
return null;
}

return conn;
}

public static void main (String args[])
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

if(load_mysql_driver())
{
System.out.println("Unable to load MySQL Driver!");
return;
}

conn = connect_mysql("hostname", "database", "username", "password");

try
{
stmt = conn.createStatement();
rs = stmt.executeQuery( "SELECT * FROM myTable t" );

while(rs.next())
{
//Do something with the results
}
}
catch(SQLException e)
{
System.err.println(e);
return;
}
}


That's the very basics. If you google for MySQL java examples you
should find a bunch.

Reply With Quote
  #4 (permalink)  
Old 12-05-2006
jcsnippets.atspace.com
 
Posts: n/a
Default Re: Connecting to mysql via Java

yankeerivera@yahoo.com wrote in news:1165255854.409987.82500
@f1g2000cwa.googlegroups.com:

> Hi, I am new to mysql. I need to connect to a mysql server from a Java
> application. Can someone share some sample code or point me to the
> right place for a sample?
>
> I am assuming that I'll have to download a Mysql driver and I'll be
> able to use the usual java.sql.* classes.


Hi,

All the steps you need to take to connect to a MySQL database, and some
sample code can be found here:
http://jcsnippets.atspace.com/java/d...-database.html

Best regards,

JayCee
--
http://jcsnippets.atspace.com/
a collection of source code, tips and tricks
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:05 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0