JDBC24homework
编写程序:
创建一个类DBTools,在DBTools中创建一个方法find,find方法用于对数据库进行查询操作,现在要求将结果集封装成数组线性表嵌套数组的形式:
ArrayList<String[]> rsList = new ArrayList<String[]>()形式
其中: String[] 用于存储一行记录的所有字段(的值)
rsList存储所有的数组(String[]),即记录的总数
1,需要先创建一个用户的基本信息作为一个类,这样在其他类中可以直接定义为此类类型的对象。
1 package com.xt.java.base25; 2 3 public class User { 4 5 private String userId; 6 7 private String userName; 8 9 private String password; 10 11 private String email; 12 13 private String telNo; 14 15 private String gender; 16 17 private String memo; 18 19 private double salary; 20 21 /** 22 * @return the userId 23 */ 24 public String getUserId() { 25 return userId; 26 } 27 28 /** 29 * @param userId the userId to set 30 */ 31 public void setUserId(String userId) { 32 this.userId = userId; 33 } 34 35 /** 36 * @return the userName 37 */ 38 public String getUserName() { 39 return userName; 40 } 41 42 /** 43 * @param userName the userName to set 44 */ 45 public void setUserName(String userName) { 46 this.userName = userName; 47 } 48 49 /** 50 * @return the password 51 */ 52 public String getPassword() { 53 return password; 54 } 55 56 /** 57 * @param password the password to set 58 */ 59 public void setPassword(String password) { 60 this.password = password; 61 } 62 63 /** 64 * @return the email 65 */ 66 public String getEmail() { 67 return email; 68 } 69 70 /** 71 * @param email the email to set 72 */ 73 public void setEmail(String email) { 74 this.email = email; 75 } 76 77 /** 78 * @return the telNo 79 */ 80 public String getTelNo() { 81 return telNo; 82 } 83 84 /** 85 * @param telNo the telNo to set 86 */ 87 public void setTelNo(String telNo) { 88 this.telNo = telNo; 89 } 90 91 /** 92 * @return the gender 93 */ 94 public String getGender() { 95 return gender; 96 } 97 98 /** 99 * @param gender the gender to set 100 */ 101 public void setGender(String gender) { 102 this.gender = gender; 103 } 104 105 /** 106 * @return the memo 107 */ 108 public String getMemo() { 109 return memo; 110 } 111 112 /** 113 * @param memo the memo to set 114 */ 115 public void setMemo(String memo) { 116 this.memo = memo; 117 } 118 119 /** 120 * @return the salary 121 */ 122 public double getSalary() { 123 return salary; 124 } 125 126 /** 127 * @param salary the salary to set 128 */ 129 public void setSalary(double salary) { 130 this.salary = salary; 131 } 132 133 /* (non-Javadoc) 134 * @see java.lang.Object#toString() 135 */ 136 @Override 137 public String toString() { 138 return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + ", email=" + email 139 + ", telNo=" + telNo + ", gender=" + gender + ", memo=" + memo + ", salary=" + salary + "]"; 140 } 141 142 143 144 145 }
2,创建一个数据库databases access objected和数据库建立联系直接调用,这样写一次就行了
package com.xt.java.base25; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class UserDAO { private Connection conn; private Statement stat; private String url="jdbc:mysql://localhost:3306/lyxdatabases"; private String dbDriver="com.mysql.jdbc.Driver"; private String userName="root"; private String password="1234"; //私有的构造方法,只有在本类中可以进行实例化一次,为了保护用户的隐私使用这种方法,切记!!! private UserDAO(){ } private static UserDAO uda=null; /** * 写一个方法,从方法中获得实例,只能实例化一次。 */ public static UserDAO getUserDAO(){ if(uda==null){ uda=new UserDAO(); } return uda; } public Connection getConnection() throws Exception{ try { Class.forName(dbDriver); return DriverManager.getConnection(url,userName,password); } catch (ClassNotFoundException e) { throw new ClassNotFoundException("数据库找不到驱动!!"); } catch (SQLException e) { throw new SQLException("数据库连接异常!!"); } } //关闭Connection public void closeConnection(Connection conn){ try{ if(conn!=null){ conn.close(); } }catch(Exception e){ System.out.println(e); } } //关闭Statement public void closeStatement(Statement stat){ try{ if(stat!=null){ stat.close(); } }catch(Exception e){ System.out.println(e); } } //关闭ResultSet public void closeResultSet(ResultSet rs){ try{ if(rs!=null){ rs.close(); } }catch(Exception e){ System.out.println(e); } } }
3
创建一个类DBTools,在DBTools中创建一个方法find,find方法用于对数据库进行查询操作,现在要求将结果集封装成数组线性表嵌套数组的形式:
ArrayList<String[]> rsList = new ArrayList<String[]>()形式
package com.xt.java.base25; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.Iterator; /** * 创建一个类DBTools,在DBTools中创建一个方法find,find方法用于对数据库进行查询操作, * 现在要求将结果集封装成数组线性表嵌套数组的形式: ArrayList<String[]> rsList = new ArrayList<String[]>()形式 其中: String[] 用于存储一行记录的所有字段(的值) rsList存储所有的数组(String[]),即记录的总数 * @author LENOVO * */ public class DBtools { Connection conn; Statement stat; ResultSet rs; UserDAO udao=UserDAO.getUserDAO(); public void find(String userID){ ArrayList<String> rsList=new ArrayList<String>(20); String sql="select*from user where userID="+userID; try { conn=udao.getConnection(); stat=conn.createStatement(); rs=stat.executeQuery(sql); while(rs.next()){ User user=new User(); user.setUserId(rs.getString(1)); user.setUserName(rs.getString(2)); user.setPassword(rs.getString(3)); user.setEmail(rs.getString(4)); user.setTelNo(rs.getString(5)); user.setGender(rs.getString(6)); user.setMemo(rs.getString(7)); user.setSalary(rs.getDouble(8)); rsList.add(user.toString()); } Iterator<String> iterator=rsList.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ udao.closeResultSet(rs); udao.closeStatement(stat); udao.closeConnection(conn); } } public static void main(String[] args) { DBtools dbt=new DBtools(); dbt.find("123"); } }