做洁具最好的网站,网站开发及app开发报价,wordpress手机 不适应,数字展馆公司(在上一篇文章中#xff0c;我们详细的介绍了连接数据库的方法#xff0c;以及eclipse操作数据库信息的相关方法#xff0c;在这里我们将主要讲封装。)主要内容#xff1a;一般的连接数据库测试把连接数据库的方法封装成一个类和测试一个简单的插入表实例查询数据实例封装查…(在上一篇文章中我们详细的介绍了连接数据库的方法以及eclipse操作数据库信息的相关方法在这里我们将主要讲封装。)主要内容一般的连接数据库测试把连接数据库的方法封装成一个类和测试一个简单的插入表实例查询数据实例封装查询的数据库的信息封装信息后的查询数据库一.一般的数据库连接测试1 public classTestConnection1 {2 public static void main(String[] args) throwsException {3 Class.forName(com.mysql.jdbc.Driver);4 String urljdbc:mysql://localhost:3306/test?//数据库url5 useUnicodetruecharacterEncodingUTF8;//防止乱码6 String userh4;7 String pass111;8 Connection connDriverManager.getConnection(url, user, pass);910 System.out.println(conn,成功连接数据库);11 conn.close();12 }13 }二.我们不可能每写一个处理信息功能就写一次连接这样太麻烦那么为了方便以后的应用我们通常把数据库连接封装起来。具体实现步骤如下1.定义变量private static String DRIVER_CLASS;private static String URL;private static String USERRNAME;private static String PASSWORD;2.在你建的eclipse根目录下新建一个File文件Properties文件内容为你定义的变量所指向的对象drivercom.mysql.jdbc.Driverurljdbc:mysql://localhost:3306/test? useUnicodetruecharacterEncodingUTF8userh4pass1113.构建一个Properties对象Properties pnew Properties();4. java.io下的类FileInputStream的方法FileInputStream(String name) 通过打开一个到实际文件的连接来创建一个 FileInputStream该文件通过文件系统中的路径名 name 指定。来获取这个文件里面的资料FileInputStream fisnew FileInputStream(db.properties);5. 用3构建的变量p来下载资料p.load(fis);6.利用getProperty();获取参数DRIVER_CLASSp.getProperty(driver);URLp.getProperty(url);USERRNAMEp.getProperty(user);PASSWORDp.getProperty(pass);7.写一个连接数据库的方法getConection()8.写一个关闭数据库的方法close(Connection conn)写好后代码如下1 public classjdbcutil {2 private staticString DRIVER_CLASS;3 private staticString URL;4 private staticString USERRNAME;5 private staticString PASSWORD;6 private static Properties pnewProperties();7 static{8 try{9 FileInputStream fisnew FileInputStream(db.properties);10 p.load(fis);11 DRIVER_CLASSp.getProperty(driver);12 URLp.getProperty(url);13 USERRNAMEp.getProperty(user);14 PASSWORDp.getProperty(pass);15 Class.forName(DRIVER_CLASS);16 fis.close();17 } catch(IOException e) {18 e.printStackTrace();19 } catch(ClassNotFoundException e) {20 e.printStackTrace();21 }22 }23 public staticConnection getConection(){24 Connection connnull;25 try{26 connDriverManager.getConnection(URL, USERRNAME, PASSWORD);27 }28 catch(Exception e) {29 e.printStackTrace();30 }31 returnconn;32 }33 public static voidclose(Connection conn) {34 try{35 if (conn ! null)36 conn.close();37 } catch(Exception e) {38 e.printStackTrace();39 }40 }4142 }那么封装好之后我们来写一个测试类测试连接1 public classTestConnection2 {23 public static void main(String[] args) throwsException {4 Connection connjdbcutil.getConection();//利用封装好的类名来调用连接方法便可5 System.out.println(conn,成功连接数据库);6 jdbcutil.close( conn);//同样利用类名调用关闭方法即可7 }8 }三.连接成功我们写一个简单的向数据库插入表的实例。1 public classTestDDl {23 public static voidmain(String[] args) {4 Connection connnull;5 Statement stmtnull;6 connjdbcutil.getConection();//连接数据库7 String createTableSql create table user_test1( //记住引号和单词间一定要有空格8 id int, 9 name varchar(32) , 10 password varchar(32) , 11 birthday date 12 ) ;13 try{14 stmtconn.createStatement();15 stmt.execute(createTableSql);16 } catch(SQLException e) {17 e.printStackTrace();18 }19 jdbcutil.close(null, stmt, conn);//关闭数据库20 }21 }四.我们在写一个查询数据库数据的实例。(有三种方法)1 public classTestDQL {2 public static voidmain(String[] args){3 Connection connnull;//定义为空值4 Statement stmtnull;5 ResultSet rsnull;6 String sqlselect * from employees;//sql语句7 connjdbcutil.getConection();8 try{9 stmtconn.createStatement();//创建一个Statement语句对象10 rsstmt.executeQuery(sql);//执行sql语句11 while(rs.next()){12 System.out.print(rs.getInt(1),);13 System.out.print(rs.getString(2),);//直接使用参数14 System.out.print(rs.getString(3),);15 System.out.print(rs.getString(4),);16 System.out.println(rs.getString(5));17 }18 } catch(SQLException e) {19 e.printStackTrace();20 }finally{21 jdbcutil.close(rs,stmt,conn);//关闭数据库22 }23 }24 }//第二种方法如下1 public classTestDQl2 {23 public static voidmain(String[] args) {4 Connection connnull;5 Statement stmtnull;6 ResultSet rsnull;7 String sqlselect * from employees;8 connjdbcutil.getConection();9 try{10 stmtconn.createStatement();11 rsstmt.executeQuery(sql);12 while(rs.next()){13 System.out.print(rs.getInt(userid),);//里面直接写要查找的内容名称14 System.out.print(rs.getString(employee_id),);15 System.out.print(rs.getString(last_name),);16 System.out.print(rs.getString(salary),);17 System.out.println(rs.getString(department_id));18 }19 } catch(SQLException e) {20 e.printStackTrace();21 }finally{22 jdbcutil.close(rs,stmt,conn);23 }24 }25 }1 //第三种方法如下2 public classTestDQL3 {3 public static voidmain(String[] args) {4 Connection connnull;5 Statement stmtnull;6 ResultSet rsnull;7 String sqlselect * from employees;8 connjdbcutil.getConection();9 try{10 stmtconn.createStatement();11 rsstmt.executeQuery(sql);12 while(rs.next()){13 int index1;14 System.out.print(rs.getInt(index),);15 System.out.print(rs.getString(index),);16 System.out.print(rs.getString(index),);17 System.out.print(rs.getString(index),);18 System.out.println(rs.getString(index));19 }20 } catch(SQLException e) {21 e.printStackTrace();22 }finally{23 jdbcutil.close(rs,stmt,conn);24 }25 }26 }五.在四里面我们写了查询员工资料的信息但是有的时候我们要保存起来方便之后更好的查找那怎么办呢没错封装。1 public class employees implementsSerializable {2 privateInteger userid;3 privateString employee_id;4 privateString last_name;5 privateString salary;6 privateString department_id;78 publicemployees() {9 super();10 }1112 publicemployees(String employee_id, String last_name, String salary, String department_id) {13 super();14 this.employee_id employee_id;15 this.last_name last_name;16 this.salary salary;17 this.department_id department_id;18 }1920 Override21 publicString toString() {22 return employees [userid userid , employee_id employee_id , last_name last_name23 , salary salary , department_id department_id ];24 }2526 publicInteger getUserid() {27 returnuserid;28 }2930 public voidsetUserid(Integer userid) {31 this.userid userid;32 }3334 publicString getEmployee_id() {35 returnemployee_id;36 }3738 public voidsetEmployee_id(String employee_id) {39 this.employee_id employee_id;40 }4142 publicString getLast_name() {43 returnlast_name;44 }4546 public voidsetLast_name(String last_name) {47 this.last_name last_name;48 }4950 publicString getSalary() {51 returnsalary;52 }5354 public voidsetSalary(String salary) {55 this.salary salary;56 }5758 publicString getDepartment_id() {59 returndepartment_id;60 }6162 public voidsetDepartment_id(String department_id) {63 this.department_id department_id;64 }65 }六.封装好后的查询和上面没封装之前有点变化。1 public classTestDQL4 {2 public static voidmain(String[] args) {3 Connection connnull;4 Statement stmtnull;5 ResultSet rsnull;6 List empsnew ArrayList();//构造集合对象78 String sqlselect * from employees;910 connjdbcutil.getConection();//获取数据库连接1112 try{13 stmtconn.createStatement();14 rsstmt.executeQuery(sql);15 while(rs.next()){//遍历结果集16 int index1;17 employees empnew employees();//构造员工类对象18 emp.setUserid(rs.getInt(index));//获取值19 emp.setEmployee_id(rs.getString(index));20 emp.setLast_name(rs.getString(index));21 emp.setSalary(rs.getString(index));22 emp.setDepartment_id(rs.getString(index));23 emps.add(emp);//放到集合中去24 }25 } catch(SQLException e) {26 e.printStackTrace();27 }finally{28 jdbcutil.close(rs,stmt,conn);//关闭连接29 }30 for(employees emp:emps){//遍历31 System.out.println(emp);32 }33 }34 }其实我们可以继续封装把遍历结果集给封装起来。1 public classTestDQL5 {23 public static voidmain(String[] args) {4 Connection connnull;5 Statement stmtnull;6 ResultSet rsnull;7 List empsnew ArrayList();89 String sqlselect * from employees;1011 connjdbcutil.getConection();1213 try{14 stmtconn.createStatement();15 rsstmt.executeQuery(sql);16 empsresultSetToEmployees(rs);17 } catch(SQLException e) {18 e.printStackTrace();19 }finally{20 jdbcutil.close(rs,stmt,conn);21 }22 for(employees emp:emps){23 System.out.println(emp);24 }25 }26 public static ListresultSetToEmployees(ResultSet rs){27 List empsnew ArrayList();28 try{29 while(rs.next()){30 int index1;31 employees empnewemployees();32 emp.setUserid(rs.getInt(index));33 emp.setEmployee_id(rs.getString(index));34 emp.setLast_name(rs.getString(index));35 emp.setSalary(rs.getString(index));36 emp.setDepartment_id(rs.getString(index));37 emps.add(emp);38 }39 } catch(SQLException e) {40 e.printStackTrace();41 }4243 returnemps;44 }45 }如果是一个人查询信息呢还可以这样封装。1 public classTestDQL6 {2 public static voidmain(String[] args) {3 Connection connnull;4 Statement stmtnull;5 ResultSet rsnull;6 List empsnew ArrayList();78 String sqlselect * from employees;910 connjdbcutil.getConection();1112 try{13 stmtconn.createStatement();14 rsstmt.executeQuery(sql);15 while(rs.next()){16 employees empresultSetToEmployee(rs);17 emps.add(emp);18 }19 } catch(SQLException e) {20 e.printStackTrace();21 }finally{22 jdbcutil.close(rs,stmt,conn);23 }24 for(employees emp:emps){25 System.out.println(emp);26 }27 }28 public staticemployees resultSetToEmployee(ResultSet rs){29 employees empnull;30 try{31 int index1;32 empnewemployees();33 emp.setUserid(rs.getInt(index));34 emp.setEmployee_id(rs.getString(index));35 emp.setLast_name(rs.getString(index));36 emp.setSalary(rs.getString(index));37 emp.setDepartment_id(rs.getString(index));38 } catch(SQLException e) {39 e.printStackTrace();40 }41 returnemp;42 }43 }