增删改查---体现面向对象思想
package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import domain.Person; import util.JDBCUtil; /** * 测试表的dao类 * @author ASUS * */ public class PersonDao { /** * insert */ public void insert(Person p) { Connection conn=null; Statement st=null; try { conn=JDBCUtil.openConnection(); //创建st对象 st=conn.createStatement(); String sql="insert persons(name,age) values('"+p.getName()+"',"+p.getAge()+")"; st.execute(sql); }catch(Exception e) { e.printStackTrace(); } finally { JDBCUtil.closeStatement(st); JDBCUtil.closeConnection(conn); } } /** * update */ public void update(Person p) { Connection conn=null; Statement st=null; try { conn=JDBCUtil.openConnection(); //创建st对象 st=conn.createStatement(); String sql="update persons set name='"+p.getName()+"',age="+p.getAge()+" where id="+p.getId(); st.execute(sql); }catch(Exception e) { e.printStackTrace(); } finally { JDBCUtil.closeStatement(st); JDBCUtil.closeConnection(conn); } } /** * delete */ public void insertById(Integer id) { Connection conn=null; Statement st=null; try { conn=JDBCUtil.openConnection(); //创建st对象 st=conn.createStatement(); String sql="delete from persons where id = "+id; st.execute(sql); }catch(Exception e) { e.printStackTrace(); } finally { JDBCUtil.closeStatement(st); JDBCUtil.closeConnection(conn); } } /** * select */ public Person findById(Integer id) { Connection conn=null; Statement st=null; ResultSet rs=null; try { conn=JDBCUtil.openConnection(); //创建st对象 st=conn.createStatement(); String sql="select * from persons where id = "+id; rs=st.executeQuery(sql); if(rs.next()) { Person p = new Person(); p.setId(rs.getInt("id")); p.setName(rs.getString("name")); p.setAge(rs.getInt("age")); return p; } }catch(Exception e) { e.printStackTrace(); } finally { JDBCUtil.closeResultSet(rs); JDBCUtil.closeStatement(st); JDBCUtil.closeConnection(conn); } return null; } /** * select */ public List<Person> findAll() { Connection conn=null; Statement st=null; ResultSet rs=null; try { conn=JDBCUtil.openConnection(); //创建st对象 st=conn.createStatement(); String sql="select * from persons"; rs=st.executeQuery(sql); List<Person> persons=new ArrayList<Person>(); Person p = null; while(rs.next()) { p = new Person(); p.setId(rs.getInt("id")); p.setName(rs.getString("name")); p.setAge(rs.getInt("age")); persons.add(p); } return persons; }catch(Exception e) { e.printStackTrace(); } finally { JDBCUtil.closeResultSet(rs); JDBCUtil.closeStatement(st); JDBCUtil.closeConnection(conn); } return null; } }
package domain; /** * Person * @author ASUS * */ public class Person { private Integer id; private String name; private int age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 工具类 * @author ASUS * */ public class JDBCUtil { /** * 开启连接 * @return */ public static Connection openConnection() { try { //获得连接 String url="jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC"; String username="root"; String password="wang18339401841."; return DriverManager.getConnection(url, username, password); }catch (Exception e) { e.printStackTrace(); } return null; } /** * 关闭连接 * @param conn */ public static void closeConnection(Connection conn) { try { if(conn!=null&& !conn.isClosed()) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } /** * 关闭语句 * @param conn */ public static void closeStatement(Statement st) { try { if(st!=null&& !st.isClosed()) st.close(); } catch (SQLException e) { e.printStackTrace(); } } /** * 关闭ResultSet * @param conn */ public static void closeResultSet(ResultSet rs) { try { if(rs!=null&& !rs.isClosed()) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }
package Jdbc; import dao.PersonDao; import domain.Person; public class App { public static void main(String[] args) { PersonDao dao=new PersonDao(); // Person p = new Person(); // p.setName("tomaslee"); // p.setAge(15); //dao.insert(p); // List<Person> list = dao.findAll(); // for(Person p: list){ // System.out.println(p.getId()+","+p.getName()); // } Person p=new Person(); p.setId(1); p.setName("king"); p.setAge(10); dao.update(p); System.out.println("over"); } }