这 就 是 我
最 新 公 告
站 点 日 历
最 新 日 志
最 新 回 复
最 新 留 言
博 客 搜 索
友 情 链 接
 
 
 
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();
}

}

}

阅读全文 | 回复(0) | 引用通告 | 编辑 | 收藏该日志
 


发表评论:

    昵称:
    密码:
    主页:
    标题: