jdbc 里面的 UserDAO 是什么接口?
程序代码:
package com.xxx.jdbcdemo.dao; import java.awt.List; import java.beans.Statement; import java.sql.Connection; import java.sql.Date; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.ArrayList; import javax.xml.crypto.Data; import com.xxx.jdbcdemo.db.DBCConnection; import com.xxx.jdbcdemo.javabean.User; public class UserDaoImpl { //定义一个数据库连接的对象 private DBCConnection dbConnection; //创建数据库连接的对象 public UserDaoImpl(){ dbConnection=new DBCConnection(); } //插入数据 public void insertUser(User user) throws Exception{ Connection conn=dbConnection.getConnection(); //得到执行sql语句的对象 java.sql.Statement sta=conn.createStatement(); //将日期时间格式化 String registerTime=new SimpleDateFormat("yyyy-MM-" + "ddhh:mm:ss").format(user.getRegisterTime()); //拼接sql语句 String insertSql="insert into " + "user(username,password,registerTime)"; //开始执行sql语句 int temp=sta.executeUpdate(insertSql); //关闭执行sql语句 sta.close(); dbConnection.closeConnection(conn); if(temp>0){ System.out.println("插入数据成功"); }else{ System.out.println("插入数据失败"); } } //修改数据 public void updataUserByID(User user) throws Exception{ Connection conn=dbConnection.getConnection(); //得到执行sql语句的对象 java.sql.Statement sta=conn.createStatement(); //拼接sql语句 String updateSql="update user set username='" +user.getUsername()+"'password='"+user.getPassword() +"'age="+user.getAge()+"where id"+user.getId(); //开始执行sql语句 int temp=sta.executeUpdate(updateSql); //关闭执行sql语句的对象 sta.close(); //关闭执行sql语句的对象 sta.close(); dbConnection.closeConnection(conn); if(temp>0){ System.out.println("修改数据成功"); }else{ System.out.println("修改数据失败"); } } public void deleteUserByID(int id)throws Exception{ //打开数据库 Connection conn=dbConnection.getConnection(); //得到执行sql语句的对象 java.sql.Statement sta=conn.createStatement(); //拼接sql语句 String deleteSql="delete from user where id="+id; //开始执行sql语句 int temp=sta.executeUpdate(deleteSql); sta.close(); dbConnection.closeConnection(conn); if(temp>0){ System.out.println("删除数据成功"); }else{ System.out.println("删除数据失败"); } } //查询一条数据 public User selectUserByID(int id)throws Exception{ //打开数据库连接 Connection conn=dbConnection.getConnection(); java.sql.Statement sta=conn.createStatement(); //拼接sql语句 String selectSql="select*from user where id="+id; //开始执行sql语句,返回一个结果集 ResultSet res=sta.executeQuery(selectSql); //遍历结果集 //如果有数据就获取数据 User user=null; if(res.next()){ int uid=res.getInt(1); String name=res.getString(2); String pass=res.getString(3); int age=res.getInt(4); Date date=res.getDate(5); //封装成一个user对象 user=new User(); user.setId(uid); user.setUsername(name); user.setPassword(pass); user.setAge(age); user.setRegisterTime(new java.util.Date(date.getTime())); } //关闭结果集 res.close(); //关闭执行sql的对象 sta.close(); //关闭数据路链接 dbConnection.closeConnection(conn); return user; } public java.util.List<User> selectAllUsers() throws Exception{ java.util.List<User> userlist=new ArrayList<User>(); Connection conn=dbConnection.getConnection(); java.sql.Statement sta=conn.createStatement(); String selectSql="select *from user"; ResultSet res=sta.executeQuery(selectSql); while(res.next()){ User user=new User(); user.setId(res.getInt("id")); user.setUsername(res.getString("username")); user.setPassword(res.getString("password")); user.setAge(res.getInt("age")); user.setRegisterTime(res.getDate("registerTime")); userlist.add(user); } res.close(); sta.close(); dbConnection.closeConnection(conn); return userlist; } } package com.xxx.jdbcdemo.javabean; import java.util.Date; import javax.xml.crypto.Data; public class User implements Serializable { private static final long serialiVersionUID=1L; private int id; private String username; private String password; private int age; private Date registerTime; public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getUsername(){ return username; } public void setUsername(String username){ this.username=username; } public String getPassword(){ return password; } public void setPassword(String password){ this.password=password; } public int getAge(){ return age; } public void setAge(int age){ this.age=age; } public void setRegisterTime(Date registerTime){ this.registerTime=registerTime; } public Date getRegisterTime() { // TODO 自动生成的方法存根 return registerTime; } } [code] package com.xxx.jdbcdemo.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBCConnection { //数据库驱动名称 public static final String DRIVER_NAME= "com.mysql.jdbc.Driver"; //数据库链接地址 public static final String URL="jdba:mysql://localhost:3306/test"; //登陆数据库用户名 public static final String USERNAME="root"; public static final String PASSWORD="123456"; //静态块 static{ try{ //加载数据库驱动类 Class.forName(DRIVER_NAME); }catch(ClassNotFoundException e){ throw new ExceptionInInitializerError(e); } } //获取数据库连接 public Connection getConnection(){ Connection conn=null; try{ //建立连接 conn=DriverManager.getConnection(URL,USERNAME,PASSWORD); if(conn!=null){ System.out.println("连接成功!"); }else{ System.out.println("连接失败!"); } }catch(Exception e){ e.printStackTrace(); } return conn; } //关闭数据库链接 public void closeConnection(Connection conn){ try{ if(conn!=null){ //关闭数据库 //使用connection对象调用close conn.close(); conn=null; } }catch(SQLException e){ e.printStackTrace(); } } }
[/code]