|
 |
|
|
|

J***A连接MYSQL beiyue 发表于 2011/2/12 10:10:00 |
import java.sql.*;
public class Conn {
static Connection con;
// static Statement sql;
static PreparedStatement sql;
static ResultSet res;
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("DB Driver is loaded successfully.");
}catch(java.lang.ClassNotFoundException e){
e.printStackTrace();
}
try{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
System.out.println("DB Connection is success.");
}catch(SQLException e){
e.printStackTrace();
}
return con;
}
public static void main(String args[]){
Conn c = new Conn();
con=c.getConnection();
try{
sql=con.prepareStatement("select * from user");
res=sql.executeQuery();
while(res.next()){
String id=res.getString("id");
String name=res.getString("name");
String gender=res.getString("gender");
String birthday=res.getString("birthday");
System.out.print("ID: "+id);
System.out.print(" | NAME: "+name);
System.out.print(" | Gender: "+gender);
System.out.println(" | Birthday: "+birthday);
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
|
|
发表评论:
|
 |
 |
|