JAVA对数据库进行操作,实现数据库中数据的插入,查询,更改,删除操作
1 package 数据库_向数据库插入数据;
2
3 import ;
4 import ;
5 import ;
6 import ;
7 import ;
8 import ;
9 import ;
10
11 //EmployeeOperation类用于操作数据库,以下的编写方法是JAVA软件编程的一种设计模式,称为!!!!! 单例模式,!!!!!!!
12 //方法中先判断该类的对象是否为空,只有为空才创建(new) ,因此保证该对象在程序中永远是唯一的,可以避免重复创建对象造成的系统内存被过多占用
13 public class EmployeeOperation {
14 private static EmployeeOperation instance = null;
15
16 public static EmployeeOperation getInstance() { //返回EmployeeOperation类实例的静态方法,单例模式!!!!
17 if (instance == null) {
18 instance = new EmployeeOperation();
19 }
20 return instance;
21 }
22
23 public boolean saveEmployee(Employee emp) { //向数据库中加入数据
24 boolean result = false;
25 Connection conn = null;
26 try {
27
28 conn = (); //建立数据库连接
29 String sqlInset = "insert into company.tb_employee(empId, empName, empAge, empSex) values(?, ?, ?, ?)";
30 PreparedStatement stmt = (sqlInset); //会抛出异常
31
32 (1, ()); //设置SQL语句第一个“?”的值
33 (2, ()); //设置SQL语句第二个“?”的值
34 (3, ()); //设置SQL语句第三个“?”的值
35 (4, ()); //设置SQL语句第四个“?”的值
36 int i = (); //执行插入数据操作,返回影响的行数
37 if (i == 1) {
38 result = true;
39 }
40 } catch (SQLException e) {
41 // TODO Auto-generated catch block
42 ();
43 } finally { //finally的用处是不管程序是否出现异常,都要执行finally语句,所以在此处关闭连接
44 try {
45 (); //打开一个Connection连接后,最后一定要调用它的close()方法关闭连接,以释放系统资源及数据库资源
46 } catch(SQLException e) {
47 ();
48 }
49 }
50
51 return result;
52
53 }
54
55
56 public List<Employee> selectEmployee() { //从数据库中查询所需数据
57 List<Employee> empList = new ArrayList<Employee>();
58 Connection conn = null;
59 try {
60 conn = ();
61 Statement stmt = ();
62 ResultSet rs = ("select * from company.tb_employee");//执行SQL并返回结果集
63 while (()) {
64 Employee emp = new Employee();
65 (("empId")); //从结果集rs中获取内容时,若为字符串类型的,用("string")方法
66 (("empName")); //其中str为想要从 数据库的 表 中获取的信息
67 (("empAge")); //若为int类型,用(number);
68 (("empSex"));
69 (emp);
70 }
71 } catch (Exception e) {
72 ();
73 } finally {
74 try {
75 (); //关闭连接
76 } catch (SQLException e) {
77 // TODO Auto-generated catch block
78 ();
79 }
80 }
81 return empList; //返回结果
82 }
83
84
85 public boolean updateEmployee(Employee emp) { //根据员工的编号更改员工的年龄信息
86 boolean result = false;
87 Connection conn = null;
88 try {
89 conn = ();
90 String sql = "update company.tb_employee set empAge=? where empId=?"; //update语句
91 PreparedStatement stmt = (sql);
92 (1, ()); //设置SQL语句第一个"?"的参数值
93 (2, ()); //设置SQL语句第二个"?"的参数值
94 int flag = (); //执行修改操作,返回影响的行数
95 if (flag == 1) { //修改成功返回true
96 result = true;
97 }
98 } catch(Exception e) {
99 ();
100 } finally {
101 try {
102 ();
103 } catch (SQLException e) {
104 // TODO Auto-generated catch block
105 ();
106 }
107 }
108 return result;
109 }
110
111 public boolean deleteEmployeeById(Employee emp) {
112 boolean result = false;
113 Connection conn = null;
114 try {
115 conn = ();
116 String sql = "delete from company.tb_employee where empId = ?";
117 PreparedStatement stmt = (sql);
118 (1, ());
119 int i = ();
120 if (i == 1) {
121 result = true;
122 }
123 } catch (Exception e) {
124 ();
125 } finally {
126 try {
127 ();
128 } catch (SQLException e) {
129 // TODO Auto-generated catch block
130 ();
131 }
132 }
133 return result;
134 }
135
136 }