JavaWeb前瞻—JDBC
JDBC
Java Database Connection java数据库连接。一套接口
面向接口编程。
四大参数:
//检查驱动是否存在Class.forName("com.mysql.cj.jdbc.Driver");//2.创建数据库连接String url = "jdbc:mysql://localhost:3306/students2025";//数据库连接字符串String username ="root";String password = "123456";
三种:
一.读操作
有sql注入风险
public class Test01 {public static void main(String[] args) {//查询操作Connection conn =null;try {//检查驱动是否存在Class.forName("com.mysql.cj.jdbc.Driver");//2.创建数据库连接String url = "jdbc:mysql://localhost:3306/students2025";//数据库连接字符串String username ="root";String password = "123456";conn = DriverManager.getConnection(url,username,password);//3.创建数据库执行器Statement stmt = conn.createStatement();//4.发送sql,执行sqlString sql = "select id,stu_id,name,pinyin,sex,birthday,height,weight from t_student";//5.返回结果集ResultSet rs = stmt.executeQuery(sql);//6 从返回的结果集中获取想要的数据while (rs.next()){Integer id = rs.getInt("id");String stuId = rs.getString("stu_id");String name = rs.getString("name");String pinyin = rs.getString("pinyin");String sex = rs.getString("sex");Date birthday = rs.getDate("birthday");Integer height = rs.getInt("height");BigDecimal weight = rs.getBigDecimal("weight");System.out.println(id+" "+stuId+" "+name+" "+pinyin+" "+sex+" "+birthday+" "+height+" "+weight);}} catch (ClassNotFoundException e) {System.out.println("mysql驱动不存在");} catch (SQLException e) {System.out.println("获取数据库连接失败");}finally {//关闭数据库连接if(conn!=null){try {conn.close();} catch (SQLException e) {throw new RuntimeException(e);}}}}
}
二.读操作
PreparedStatement ps = conn.prepareStatement(sql);
避免sql注入
public class Test02 {public static void main(String[] args) {String url = "jdbc:mysql://localhost:3306/students2025";String user = "root";String password ="123456";try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {System.out.println("11111");}//try()里的代码,相当于在finally里关闭----自动关闭try(Connection conn = DriverManager.getConnection(url,user,password);) {String sql ="select id,stu_id,name,sex,birthday,weight from t_student where weight>? limit ?,?";//避免sql注入PreparedStatement ps = conn.prepareStatement(sql);ps.setFloat(1,50.5f);ps.setInt(2,10);ps.setInt(3,5);//无需再指定sqlResultSet rs =ps.executeQuery();while (rs.next()){int id = rs.getInt("id");String stuId = rs.getString("stu_id");String sex = rs.getString("sex");Date birthday = rs.getDate("birthday");System.out.println(id+stuId+sex+birthday);}} catch (Exception e) {System.out.println("11111");}}
}
三.写操作
public class Test03 {public static void main(String[] args) {try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {throw new RuntimeException(e);}String url = "jdbc:mysql://localhost:3306/students2025";String user = "root";String password ="123456";try (Connection conn = DriverManager.getConnection(url,user,password);){//insert into t_student (id,stu_id)values(?,?)String sql = "DELETE from t_student where height >? and weight >? ";PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1,180);ps.setBigDecimal(2, BigDecimal.valueOf(90));//写操作:增删改都是excutUpdateint rows = ps.executeUpdate();System.out.println(rows);} catch (SQLException e) {throw new RuntimeException(e);}}
}
尚待更新…2025-7-30