1、创建mysql测试表
2、按下图创建3个文件夹与3个类
3、三个类的代码
PersionDao :目录(Data Access Object), 数据访问对象是第一个面向对象的数据库接口
1 package com.test.dao; 2 3 import com.test.entity.Person; 4 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 10 /**11 * Created by wdw on 2017/9/7.12 */13 public class PersonDao {14 15 private final static String DRIVER = "com.mysql.jdbc.Driver";16 private final static String URL = "jdbc:mysql://localhost:3306/wdw";17 private final static String USERNAME = "root";18 private final static String PASSWORD = "123456";19 20 /**21 * 添加一个人22 *23 * @param person24 * @return boolean25 */26 public boolean insert(Person person) {27 boolean flag = false;//28 try {29 Class.forName(DRIVER);//加载数据库驱动30 java.sql.Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);31 // 使用jar包中的connection接口,并通过DriveManager类的静态方法getConnection()创建连接对象conn32 String sql = "INSERT INTO sys_users(id,name,code) VALUES(?,?,?)";//编写sql语句33 PreparedStatement ps = conn.prepareStatement(sql);创建预处理对象ps34 ps.setInt(1, person.getId());35 ps.setString(2, person.getName());36 ps.setString(3, person.getCode());37 38 if (ps.executeUpdate() > 0) {39 flag = true;40 }41 System.out.print(flag);42 ps.close();43 conn.close();44 45 } catch (ClassNotFoundException e) {46 47 e.printStackTrace();48 } catch (SQLException e) {49 e.printStackTrace();50 }51 return flag;52 }53 54 /**55 * 添加一个人56 *57 * @param id58 * @return boolean59 */60 public Person selectById(String id) {61 Person p = new Person();62 try {63 Class.forName(DRIVER);//加载数据库驱动64 java.sql.Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);65 String sql = "delete sys_users where id=" + id;//编写sql语句66 PreparedStatement ps = conn.prepareStatement(sql);创建预处理对象ps67 ResultSet r = ps.executeQuery();68 while (r.next()) {69 p.setId(r.getInt("id"));70 p.setName((r.getString("code")));71 p.setCode(r.getString("name"));72 }73 74 ps.close();75 conn.close();76 77 } catch (ClassNotFoundException e) {78 79 e.printStackTrace();80 } catch (SQLException e) {81 e.printStackTrace();82 }83 return p;84 }85 86 }
entity:entity目录(实体),实体就是一个特定的软件模块。
1 package com.test.entity; 2 3 /** 4 * Created by wdw on 2017/9/7. 5 */ 6 public class Person { 7 8 private int id; 9 private String code;10 private String name;11 12 public int getId() {13 return id;14 }15 16 public void setId(int id) {17 this.id = id;18 }19 20 public String getCode(){21 return this.code;22 23 }24 public void setCode(String code)25 {26 this.code=code;27 28 }29 public String getName(){30 return this.name;31 32 }33 public void setName(String name)34 {35 this.name=name;36 37 }38 }
test:test目录,最终的试行测试。
1 package com.test.test; 2 3 import com.test.dao.PersonDao; 4 import com.test.entity.Person; 5 6 /** 7 * Created by wdw on 2017/9/7. 8 */ 9 public class test {10 public static void main(String[] args) {11 Person ps = new Person();12 ps.setId(1);13 ps.setCode("编号");14 ps.setName("张三");15 PersonDao s = new PersonDao();16 boolean aResult = s.insert(ps); //add a use17 boolean uResult = s.insert(ps); //delete a use18 boolean dResult = s.insert(ps); //delete a use19 }20 21 }
4、运行测试,记得在main断点。