Java Reference
In-Depth Information
Example 15.1 Simple sample program using JDBC for MySQL
import java.sql.*;
public class
MyCon
{
public static void
main(String [] args)
{
try {
// A simple connection example looks like this:
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://host.domain.com/test"+
"?user=blah&password=blah";
Connection conn = DriverManager.getConnection(url);
// query
String mySQL = "SELECT id, pw FROM Users WHERE name = ?";
PreparedStatement stmt = conn.prepareStatement(mySQL);
stmt.setString(1, args[0]);
// execute the query
ResultSet rs = stmt.executeQuery();
// read the results
while(rs.next()) {
int id = rs.getInt("id");
String pw = rs.getString("pw");
System.out.println("id="+id);
}
} catch (Exception e) {
System.out.println("Exception: "+e);
e.printStackTrace();
}
} // main
} // class MyCon
Search WWH ::




Custom Search