一、实现的步骤:
(1)加载驱动
Class.forName("com.mysql.jdbc.Driver");
(2)创建连接:
connection = DriverManager.getConnection ("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" + "characterEncoding=utf-8&user=" + "root&password=123");
(3)写SQL
String sql="select * from userinfo";
String sql="insert into userinfo (username,password) values (?,?)";
(4)得到staement对象
statement=connection.prepareStatement(sql);
(5)执行SQL得到的结果集
rs=((PreparedStatement) statement).executeQuery();
(6)处理结果集
if(rs.next()) { System.out.println(rs.getString("username")); }
(7)关闭资源
if(rs!=null){ try{ rs.close(); }catch(Exception e){ e.printStackTrace(); } rs=null; } if(st!=null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
二、实验的方法的介绍
ResultSet对象的操作:
1、获取行
ResultSet提供了对结果集进行滚动的方法:
next():移动到下一行
Previous():移动到前一行
absolute(int row):移动到指定行
beforeFirst():移动resultSet的最前面。
afterLast() :移动到resultSet的最后面。
2、获取值
ResultSet既然用于封装执行结果的,所以该对象提供的都是用于获取数据的get方法:
获取任意类型的数据
getObject(int index)
getObject(string columnName)
获取指定类型的数据,例如:
getString(int index)
getString(String columnName)
附加:
常用数据类型转换:
SQL类型 Jdbc对应方法 返回类型
bit(1),bit(n) getBoolean,getBytes() Boolean,byte[]
tinyint getByte() Byte
smallint getShort() Short
int getInt Int
bigint getLong() Long
char,varchar,longvarchar getString String
text(clob) blob getClob(),getblob() Clob,blob
date getDate() java.sql.Date
time getTime() java.sql.Time
timestamp getTimestamp java.sql.Timestamp
三、具体的实现
(1)测试类的实现
package com.zhongraunguoji; import com.zhongraunguoji.bena.UserInfo; import com.zhongraunguoji.dao.UserInfoDao; import java.sql.*; import java.util.List; public class test { public static void main(String[] args) throws ClassNotFoundException, SQLException { UserInfoDao userInfoDao=new UserInfoDao(); List<UserInfo> list=userInfoDao.findAll(); System.out.println(list); /*userInfoDao.Insert(); List<UserInfo> list1=userInfoDao.findAll(); System.out.println(list1);*/ /* userInfoDao.delete(); List<UserInfo> list2=userInfoDao.findAll(); System.out.println(list2);*/ /*userInfoDao.update(); List<UserInfo> list3=userInfoDao.findAll(); System.out.println(list3);*/ userInfoDao.select(); } }
(2)工具类的实现
package com.zhongraunguoji.util; import java.sql.*; public class DButil { public static Connection getConnection(){ Connection connection=null; try { //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //2.创建连接 connection = DriverManager.getConnection ("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" + "characterEncoding=utf-8&user=" + "root&password=123"); //System.out.println("创建连接成功"); }catch(Exception e){ System.out.println("连接失败"); e.printStackTrace(); } return connection; } public static void close(Connection conn, Statement st, ResultSet rs){ if(rs!=null){ try{ rs.close(); }catch(Exception e){ e.printStackTrace(); } rs=null; } if(st!=null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
(3)实体类的创建
package com.zhongraunguoji.bena; public class UserInfo { private int user_id; private String username; private String password; public int getUser_id() { return user_id; } public String getUsername() { return username; } public String getPassword() { return password; } public void setUser_id(int user_id) { this.user_id = user_id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserInfo{" + "user_id=" + user_id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
(4)持久层的实现
package com.zhongraunguoji.dao; import com.zhongraunguoji.bena.UserInfo; import com.zhongraunguoji.util.DButil; import java.sql.*; import java.util.ArrayList; import java.util.List; public class UserInfoDao { public List<UserInfo> findAll(){ ResultSet rs=null; PreparedStatement statement=null; Connection connection=null; List<UserInfo> list=new ArrayList<>(); try { connection= DButil.getConnection(); String sql="select * from userinfo"; //4.得到statement对象 statement = connection.prepareStatement(sql); //5.执行sql得到结果集 rs = statement.executeQuery(); //6.处理结果集 while (rs.next()){ UserInfo userInfo=new UserInfo(); userInfo.setUser_id(rs.getInt(1)); userInfo.setUsername(rs.getString(2)); userInfo.setPassword(rs.getString(3)); list.add(userInfo); } } catch (Exception e) { e.printStackTrace(); }finally { //7.关闭资源 DButil.close(connection,statement,rs); } return list; } public void Insert(){ Connection connection=null; Statement statement=null; ResultSet rs=null; String sql="insert into userinfo (username,password) values (?,?)"; try { connection=DButil.getConnection(); statement=connection.prepareStatement(sql); ((PreparedStatement) statement).setString(1,"sdg"); ((PreparedStatement) statement).setString(2,"123"); ((PreparedStatement) statement).executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { DButil.close(connection,statement,rs); } } public void delete(){ Connection connection=null; Statement statement=null; ResultSet rs=null; String sql="delete from userinfo where id=2"; try { connection=DButil.getConnection(); statement=connection.prepareStatement(sql); ((PreparedStatement) statement).executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { DButil.close(connection,statement,rs); } } public void update(){ Connection connection=null; Statement statement=null; ResultSet rs=null; String sql="update userinfo set username='update' where id=3"; try { connection=DButil.getConnection(); statement=connection.prepareStatement(sql); ((PreparedStatement) statement).executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { DButil.close(connection,statement,rs); } } public void select(){ Connection connection=null; Statement statement=null; ResultSet rs=null; String sql="select * from userinfo where id=4"; try { connection=DButil.getConnection(); statement=connection.prepareStatement(sql); rs=((PreparedStatement) statement).executeQuery(); if(rs.next()) { System.out.println(rs.getString("username")); } } catch (SQLException e) { e.printStackTrace(); }finally { DButil.close(connection,statement,rs); } } }