By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,218 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

FYI: Connect and Select from Java on Linux

P: n/a
I found the following interesting and wanted to pass it along
Reference (TIP 12):
http://prdownloads.sourceforge.net/s...l.txt?download

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")
// REF: ttp://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");
}
finally
{
if (conn != null)
{
try
{
conn.close ( );
// System.out.println
("Disconnected"); /* for debugging */
}
catch (Exception e) { /* ignore close
errors */ }
}
}
}

}

//end program.

Anyway, I thought I'd pass it along.

Regards,

Mike Chirico
Jul 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.