 |
|
 |
| |
Developer on focus
Anton Zamov is a dipl. engineer with more than 6 years of active professional
experience and many awards ...
read more>>
|
|
 |
 |
 |
|
 |
|
 |
| |
|
MySQL: Connect and Select from Java
|
Connect and Select from Java
//
// mysql-connector-java-3.0.11-stable-bin.jar or later must be downloaded
// and installed from:
// http://dev.mysql.com/downloads/connector/j/3.0.html
//
// Edit to include your username and password:
// String userName = "yourusername";
// String password = "passord";
//
//
// Compile:
// javac Select.java
// (or you can also use GCC's gcj "gcj -C Select.java")
// http://gcc.gnu.org/onlinedocs/gcj/
// http://www.linuxjournal.com/article.php?sid=4860
// http://gcc.gnu.org/java/papers/cni/t1.html
//
// Run:
// java Select
//
//
// The program above assumes "exams" has been created in
// the "test" database. If you create "exams" change the
// select statment to read:
// SELECT pkey,name,exam,score FROM yourdatabase.exams
//
//
// CREATE TABLE exams (
// pkey int(11) NOT NULL auto_increment,
// name varchar(15),
// exam int,
// score int,
// PRIMARY KEY (pkey)
//
// );
//
// insert into exams (name,exam,score) values ('Bob',1,75);
// insert into exams (name,exam,score) values ('Bob',2,77);
// insert into exams (name,exam,score) values ('Bob',3,78);
// insert into exams (name,exam,score) values ('Bob',4,80);
//
// insert into exams (name,exam,score) values ('Sue',1,90);
// insert into exams (name,exam,score) values ('Sue',2,97);
// insert into exams (name,exam,score) values ('Sue',3,98);
// insert into exams (name,exam,score) values ('Sue',4,99);
//
import java.sql.*;
public class Select
{
public static void main (String[ ] args)
{
Connection conn = null;
String url = "jdbc:mysql://localhost/";
String userName = "username1";
String password = "password1";
try
{
Class.forName ("com.mysql.jdbc.Driver").newInstance ( );
conn = DriverManager.getConnection (url, userName, password);
// System.out.println ("Connected");
Statement s = conn.createStatement ( );
s.executeQuery ("SELECT pkey,name,exam,score FROM test.exams");
ResultSet rs = s.getResultSet ( );
int count = 0;
while (rs.next ( )) // loop through rows of result set
{
int pkey = rs.getInt (1);
String name = rs.getString(2);
int exam = rs.getInt(3);
int score = rs.getInt(4);
++count;
System.out.println (count + ",inum: " + pkey + ",name: " +
name + ",exam: " + exam + ",score: " + score );
}
rs.close ( ); // close result set
s.close ( ); // close statement
System.out.println (count + " rows were returned");
}
catch (Exception e)
{
System.err.println ("Cannot connect to server"+e);
}
finally
{
if (conn != null)
{
try
{
conn.close ( );
// System.out.println ("Disconnected"); /* for debugging */
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
|
About the author of this programming example or tutorial:
Mike Chirico (mchirico@users.sourceforge.net)
Copyright (c) 2004 (GPU Free Documentation License)
Last Updated: Tue Jul 20 12:14:51 EDT 2004
|
|
|
 |
 |
 |
|
|