dao层和DBUtil层
dao.java
1 import java.sql.Connection; 2 import java.sql.ResultSet; 3 import java.sql.SQLException; 4 import java.sql.Statement; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 9 import com.User; 10 11 public class dao 12 { 13 14 //添加 15 //添加入Grade表里 16 17 public boolean insert(Grade grade) 18 { 19 String sql="insert into test(hometype,roomtype,square,roomnumber,name,ID,sex,nation,xueli) values('"+grade.getroomtype() 20 +"','"+grade.getroomtype()+"','"+grade.getsquare()+"','"+grade.getroomnumber()+"','"+grade.getname()+"','"+grade.getID() 21 +"','"+grade.getsex()+"','"+grade.getNation()+"','"+grade.getxueli()+"')"; 22 System.out.println(sql); 23 //insert语句: 24 //insert INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); 25 //注意:insert这里 values(100,'李泽宇','"+bianliang+"'),数字可以直接上,字符串的话用 '',变量的话要用 '"++"' 26 27 Connection conn=DBUtil.getConn();//数据库连接,加载驱动 28 Statement state=null;//数据库操作 29 try 30 { 31 state=conn.createStatement();//实例化Statement对象,方便对sql语句进行操作 32 state.executeUpdate(sql); 33 //执行数据库更新操作用于执行INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语言)语句, 34 //例如CREATETABLE和DROPTABLE,(创建表和删除表) 35 }catch(Exception e)//当try语句中出现异常时,会执行catch中的语句 36 { 37 e.printStackTrace();//捕获异常的语句 38 } 39 finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。 40 { 41 DBUtil.close(state, conn); //close释放资源 42 } 43 return false; 44 } 45 46 //删除 47 //根据姓名删除 48 49 public boolean delete(String name) 50 { 51 String sql="delete from test where name='"+name+"'" ; 52 //删除语句: 53 //delete from table_name where some_colume=some_value 54 55 Connection conn=DBUtil.getConn(); 56 Statement state=null; 57 58 try 59 { 60 state=conn.createStatement(); 61 state.executeUpdate(sql); 62 }catch (Exception e) 63 { 64 e.printStackTrace(); 65 } 66 finally 67 { 68 DBUtil.close(state, conn); 69 70 } 71 return true; 72 } 73 // 74 //修改 75 //根据姓名修改 76 77 public boolean update(Grade grade) 78 { 79 String sql="update test set sex='"+grade.getsex()+"',nation='"+grade.getNation() 80 +"',hometype='"+grade.gethometype()+"',roomtype='"+grade.getroomtype() 81 +"',roomnumber='"+grade.getroomnumber()+"',square='"+grade.getsquare() 82 +"',ID='"+grade.getID()+"',name="+grade.getname()+"',xueli="+grade.getxueli(); 83 //update语句: 84 //update table_name set column1=value1,column2=value2 where some_column=some_value; 85 86 Connection conn=DBUtil.getConn(); 87 Statement state=null; 88 89 try 90 { 91 state=conn.createStatement(); 92 state.executeUpdate(sql); 93 }catch (SQLException e) 94 { 95 e.printStackTrace(); 96 } 97 finally 98 { 99 DBUtil.close(state, conn); 100 } 101 return false; 102 } 103 104 //查询 105 //查询全部并存入list 106 //List指的是集合.<>是泛型,里面指定了这个集合中存放的是什么数据 107 //泛型是程序设计语言的一种特性。 108 //允许程序员在强类型程序设计语言中编写代码时定义一些可变部分, 109 //但是,那些部分在使用前必须作出指明。 110 111 public List<Grade> list() 112 { 113 String sql="select * from test "; 114 //select语句 115 //select * from tabel_name 这是查询所有,若要查询某一列 116 //select column1_name,column2_name,column3_name from table_name 117 List<Grade>list=new ArrayList<>(); 118 //给集合list创造(new)一个存储空间用于存放数据 119 120 Connection conn=DBUtil.getConn(); 121 Statement state=null; 122 ResultSet rs=null; 123 124 try 125 { 126 state=conn.createStatement(); 127 rs=state.executeQuery(sql); 128 Grade grade=null; 129 while(rs.next()) //这里表示,当rs.next()有值得时候,即收到查询结果 130 { 131 String NAME=rs.getString("name"); //注意:这里用双引号,ID是表grade_student里的ID列 132 String sex=rs.getString("sex"); 133 String nation=rs.getString("nation"); 134 String ID=rs.getString("ID"); 135 String hometype=rs.getString("homeroom"); 136 String roomtype=rs.getString("roomtype"); 137 String roomnumber=rs.getString("roomnumber"); 138 String xueli=rs.getString("xueli"); 139 String square=rs.getString("square"); 140 grade=new Grade(hometype,roomtype,square,roomnumber,NAME,ID,sex,nation,xueli); 141 list.add(grade); //表示,把bean里的数据存入到list当中 142 } 143 } 144 catch (SQLException e) 145 { 146 e.printStackTrace(); 147 } 148 finally 149 { 150 DBUtil.close(state, conn); 151 } 152 return list; 153 } 154 public boolean add1(User user) { 155 // TODO Auto-generated method stub 156 String sql = "SELECT * FROM user WHERE username='"+user.getUsername()+"' and password='"+user.getPassword()+"' "; 157 Connection conn = DBUtil.getConn(); 158 Statement state = null; 159 boolean f = false; 160 ResultSet RS = null; 161 162 try { 163 state = conn.createStatement(); 164 RS=state.executeQuery(sql); 165 if(RS.next()) { 166 f=true; 167 } 168 } catch (Exception e) { 169 e.printStackTrace(); 170 } finally { 171 172 DBUtil.close(state, conn); 173 } 174 return f; 175 176 } 177 public Grade selectByName(String name) { 178 // TODO Auto-generated method stub 179 String sql="select * from test where name like '%"+name+"%'"; 180 Connection conn=DBUtil.getConn(); 181 Statement state=null; 182 ResultSet rs=null; 183 184 try { 185 state=conn.createStatement(); 186 rs=state.executeQuery(sql); 187 188 while(rs.next()) { 189 String name1=rs.getString("name"); //注意:这里用双引号,ID是表grade_student里的ID列 190 String sex=rs.getString("sex"); 191 String nation=rs.getString("nation"); 192 String ID=rs.getString("ID"); 193 String hometype=rs.getString("homeroom"); 194 String roomtype=rs.getString("roomtype"); 195 String roomnumber=rs.getString("roomnumber"); 196 String square=rs.getString("square"); 197 String xueli=rs.getString("xueli"); 198 Grade grade=new Grade(hometype,roomtype,square,roomnumber,name1,ID,sex,nation,xueli); 199 200 return grade; 201 } 202 } catch (SQLException e) { 203 // TODO Auto-generated catch block 204 e.printStackTrace(); 205 }finally { 206 DBUtil.close(rs, state, conn); 207 } 208 209 return null; 210 211 } 212 213 }
DBUtil层
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 7 8 public class DBUtil { 9 //联结字符串 //数据库名test 10 public static String db_url = "jdbc:mysql://localhost:3306/room?serverTimezone=UTC"; 11 //数据库用户名 12 public static String db_user = "root"; 13 //数据库密码名 14 public static String db_pass = "123asd..00"; 15 16 public static Connection getConn () { 17 18 //声明与数据库的连接并实例化为null 19 Connection conn = null; 20 21 try { 22 //驱动程序名" 23 Class.forName("com.mysql.cj.jdbc.Driver");//连接数据库 24 //具体地连接到数据库——联接字符串(数据库名),联接用户名,联接密码名 25 conn = DriverManager.getConnection(db_url, db_user, db_pass); 26 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 31 return conn; 32 } 33 34 35 36 /** 37 * 关闭连接 38 * @param state 39 * @param conn 40 */ 41 public static void close (Statement state, Connection conn) { 42 if (state != null) { 43 try { 44 state.close(); 45 } catch (SQLException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 if (conn != null) { 51 try { 52 conn.close(); 53 } catch (SQLException e) { 54 e.printStackTrace(); 55 } 56 } 57 } 58 59 public static void close (ResultSet rs, Statement state, Connection conn) { 60 if (rs != null) { 61 try { 62 rs.close(); 63 } catch (SQLException e) { 64 e.printStackTrace(); 65 } 66 } 67 68 if (state != null) { 69 try { 70 state.close(); 71 } catch (SQLException e) { 72 e.printStackTrace(); 73 } 74 } 75 76 if (conn != null) { 77 try { 78 conn.close(); 79 } catch (SQLException e) { 80 e.printStackTrace(); 81 } 82 } 83 } 84 85 }