java 连接mysql增删改查
时间:2021-07-01 10:21:17
帮助过:3人阅读
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 person
24 * @return boolean
25 */
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()创建连接对象conn
32 String sql = "INSERT INTO sys_users(id,name,code) VALUES(?,?,?)";
//编写sql语句
33 PreparedStatement ps = conn.prepareStatement(sql);
////创建预处理对象ps
34 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 id
58 * @return boolean
59 */
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);
////创建预处理对象ps
67 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 }
View Code
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 }
View Code
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 use
17 boolean uResult = s.insert(ps); //delete a use
18 boolean dResult = s.insert(ps); //delete a use
19 }
20
21 }
View Code
4、运行测试,记得在main断点。
java 连接mysql增删改查
标签:exception val open 运行 cli splay sel statement rest