import java.sql.*;
public class JDBCExample {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/gbooks";
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "select * from admin";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", webtitle : " + rs.getString("webtitle"));
System.out.print(", adminhttp : " + rs.getString("adminhttp"));
System.out.print(", adminname : " + rs.getString("adminname"));
System.out.print(", adminpass : " + rs.getString("adminpass"));
System.out.print(", adminmail : " + rs.getString("adminmail"));
System.out.print(", webadvice : " + rs.getString("adminmail"));
System.out.println(", webadvice : " + rs.getString("webadvice"));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}
}
****************************************************************************************【来自我另一博文】