//实体类不仔细描述了
public User{
private int id;
private String name;
...
...
}
public DBHelp{
public static String url = "jdbc:mysql://localhost:3306/blog" ;
public static String user = "root" ;
public static String password = "" ;
public static String driver = "com.mysql.jdbc.Driver" ;
public static PreparedStatement ps = null ;
public static Connection conn = null ;
public static void getConnection (){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
}catch (SQLException e) {
e.printStackTrace();
}
}
public void close (){
try {
ps.close();
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
----------
public UserDemoCurrd extends DBHelp{
public ResultSet rs = null ;
public user getUser (){
getConnection();
User user = new User();
try {
String sql = "select * from t_sys_user where id = 1" ;
conn = ps.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()){
user.setId(rs.getInt("id" ));
user.setName(rs.getString("name" ));
user.setContact(rs.getString("contact" ));
user.setEmail(rs.getString("email" ));
user.setRemark(rs.getString("remark" ));
user.setTelephone(rs.getString("telephone" ));
}
}catch (SQLException e) {
e.printStackTrace();
}
close();
return user;
}
}
public UserTest{
public static void main (String[] a){
UserDemoCurrd demo = new UserDemoCurrd();
demo.getUser();
System.out .println(user.getName);
}
}