准备工作
1新建po类:User
private int id; private String name;private String pwd; set,get方法省略
2 新建UserDao类,存放增删改查的相关方法,并且定义链接数据库的相关性信息(链接地址,用户名,密码);
public class UserDao {private static String jdbcDriver = "com.mysql.jdbc.Driver";private static String jdbcUrl="jdbc:mysql://localhost:3306/homework";private static String jdbcuser="root";private static String jdbcpwd="123";public boolean ChaRu(User user){ //插入数据boolean flag=true;return flag;}public boolean XiuGai(User user){ //修改数据boolean flag=true;return flag;}public boolean ShanChu(int id){ //删除数据boolean flag=true;return flag;}public List
3 在数据库中创建表user,表中设置,id int
name varchar(20)
pwd varchar(20)
1 插入数据
在进行增删改查操作结束之后,一定要关闭conn,st,rs(仅查找时用到此对象)对象。
public boolean ChaRu(User user){boolean flag=true;Connection conn= null;Statement st=null;String sql="insert into user(name,pwd) values('"+user.getName()+"','"+user.getPwd()+"')"; //插入数据的SQL语句 try {Class.forName(jdbcDriver); // 加载驱动器conn=DriverManager.getConnection(jdbcUrl, jdbcuser, jdbcpwd); // 驱动利用驱动地址,数据库用户名,密码创建连接st=conn.createStatement(); // 连接创建数据库操作对象,Statement是执行数据库的重要方法int i=st.executeUpdate(sql); // Statement 对象利用executeUpdate()方法执行sql。if(i==0){ flag=false; // 后面测试用到flag变量,前面执行sql语句,如果成功i不为0,true代表执行成功,false代表失败。}} catch (ClassNotFoundException e) { //加载驱动器失败异常// TODO Auto-generated catch block
e.printStackTrace();} catch (SQLException e) { //创建链接失败异常// TODO Auto-generated catch block
e.printStackTrace();}finally{if(conn!=null){ // 最后关闭连接try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}}if(st!=null){ //关闭Statementtry {st.close();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}return flag; // 返回flag}
public static void main(String[] args) {User user=new User(); //初始化 User类 对象user可以调用set,get方法设置或者获得数据。UserDao userDao = new UserDao(); //初始化UserDao 类,可以调用增删改差的各种方法。
user.setName("lbzz"); // 设置姓名user.setPwd("123"); // 设置密码boolean flag=userDao.ChaRu(user); //执行插入操作,返回值赋给flagif(flag){ //根据flag判断执行是否成功。System.out.println("插入成功");}else{System.out.println("插入失败");}}
2 修改数据
public boolean XiuGai(User user){boolean flag=true;Connection conn= null;Statement st= null;String sql= "update user set pwd='"+user.getPwd()+"' where name='"+user.getName()+"'";System.out.println(sql);try {Class.forName(jdbcDriver);conn=DriverManager.getConnection(jdbcUrl, jdbcuser, jdbcpwd);st=conn.createStatement();int i=st.executeUpdate(sql);if(i==0){flag=false;}} catch (ClassNotFoundException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally{if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}}if(st!=null){try {st.close();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}return flag;}
测试
public static void main(String[] args) {User user= new User();UserDao userDao= new UserDao();user.setName("lbzz");user.setPwd("lbzz");boolean flag=userDao.XiuGai(user);if(flag){System.out.println("修改成功");}else{System.out.println("修改失败");}}
3 删除记录
public boolean ShanChu(int id){boolean flag=true;Connection conn= null;Statement st= null;String sql="delete from user where id="+id;try {Class.forName(jdbcDriver);conn=DriverManager.getConnection(jdbcUrl, jdbcuser, jdbcpwd);st=conn.createStatement();int i=st.executeUpdate(sql);if(i==0){flag=false;}} catch (ClassNotFoundException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally{if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}}if(st!=null){try {st.close();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}return flag;}
测试
public static void main(String[] args) {UserDao userDao = new UserDao();boolean flag= userDao.ShanChu(1);if(flag){System.out.println("删除成功");}else{System.out.println("删除失败");}
}
4 查找数据
前面增删改,程序除了sql语句不同外,其余相差无几,查找与前三个程序略有不同。
public List 关闭conn,st,rs }return list; //返回list}
e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally{
测试
public static void main(String[] args) {UserDao userDao = new UserDao();List