import java.sql.*;
class Mysql{
private String url="jdbc:mysql://localhost:3306/jdbcTest";
private String username = "root";
private String password = "139689";
public Connection getConnection(){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,username,password);
return con;
}catch(ClassNotFoundException e){
System.out.println("加载类驱动错误");
System.out.println(e);
}catch(Exception e){
System.out.println(e);
}
return null;
}
public ResultSet executeSql(String sql){
Connection con;
Statement stmt;
try{
con = getConnection();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
}catch(SQLException e){
System.out.println("获取数据失败");
}
return null;
}
}
public class jdbcTest{
public static void main(String[] args){
Mysql mysql = new Mysql();
Connection con = null;
ResultSet rs = null;
rs = mysql.executeSql("select * from user");
try{
while(rs.next()){
System.out.println(rs.getString("username"));
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
posted on 2008-10-08 17:42
poower 阅读(395)
评论(0) 编辑 收藏 所属分类:
j2ee学习笔记