JDBC:
一,jdbc的简介:
java database connectivity java数据库的连接
二,使用jdbc对数据库进行(crud)操作是固定的(加创编执释)
1,加载数据库驱动
DriverManager里面的registerDriver
//导入的是mysql.jdbc.Driver的包,但是一般不用这个方法,因为它会加载驱动两次可以查API
一般用:Class.forName("com.mysql.jdbc.Driver");//反射的方式
2,创建与数据库的连接
DriverManager里面的getConnection(String url,String username,String password);
3,编写sql
4,执行sql
Statement里面的executeQuery(String sql);
5,释放资源(关闭连接)
案列:
//加载驱动
// DriverManager.registerDriver(new Driver());//导入的是mysql.jdbc.Driver的包,但是一般不用这个方法,因为它会加载驱动两次可以查API
// 一般用:Class.forName("com.mysql.jdbc.Driver");//反射的方式
public static void main(String[] args) throws Exception {
//加载驱动
DriverManager.registerDriver(new Driver());
//创建连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb2", "root", "root");
//编写sql
String sql = "select * from user";
//执行sql
//得到statement
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
//遍历结果集得到每条记录
while(rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
int chinese = rs.getInt("chinese");
int english = rs.getInt("english");
System.out.println(id+" :: "+username+" :: "+chinese+" :: "+english);
}
//释放资源
rs.close();
stmt.close();
conn.close();
}
三,jdbc的Statement对象
1,执行sql的对象,接口,在java.sql包里面
2,执行查询操作方法
ResultSet executeQuery(String sql) ,返回查询的结果集
3,执行增加 修改 删除的方法
int executeUpdate(String sql) ,返回成功的记录数
4,执行sql语句的方法
boolean execute(String sql) ,返回是布尔类型,如果执行的是查询的操作返回true,否则返回的false
5,执行批处理的方法
addBatch(String sql):把多个sql语句放到批处理里面
int[] executeBatch():执行批处理里面的所有的sql
四,使用jdbc进行crud操作
1,实现对数据库表记录进行查询的操作
executeQuery(sql)
2,实现对数据库表记录进行增加的操作
executeUpdate(sql)
3,实现对数据库表记录进行修改的操作
executeUpdate(sql)
4,实现对数据库表记录进行删除的操作
executeUpdate(sql)
五,jdbc工具类的封装
1)
第一种,使用properties类
(1)代码
//创建properties对象
Properties p = new Properties();
//文件的输入流
InputStream in = new FileInputStream("src/db.properties");
//把文件的输入流放到对象里面
p.load(in);
String drivername = p.getProperty("drivername");
String url = p.getProperty("url");
String username = p.getProperty("username");
String password = p.getProperty("password");
第二种,使用ResourceBundle类
* 使用范围:首先读取的文件的格式需要时properties,文件需要放到src下面
(1)代码
String drivername = ResourceBundle.getBundle("db").getString("drivername");
String url = ResourceBundle.getBundle("db").getString("url");
String username = ResourceBundle.getBundle("db").getString("username");
String password = ResourceBundle.getBundle("db").getString("password");
2)
代码
public static String drivername;
public static String url;
public static String username;
public static String password;
//在类加载时候,执行读取文件的操作
static {
drivername = ResourceBundle.getBundle("db").getString("drivername");
url = ResourceBundle.getBundle("db").getString("url");
username = ResourceBundle.getBundle("db").getString("username");
password = ResourceBundle.getBundle("db").getString("password");
}
六,sql的注入和防止
防止sql的注入
(1)使用PreparedStatement预编译对象防止sql注入
(2)创建PreparedStatement对象 prepareStatement(String sql)
(3)PreparedStatement接口的父接口Statement
(4)什么是预编译
(5)步骤:
第一步,加载驱动,创建数据库的连接
第二步,编写sql
第三步,需要对sql进行预编译
第四步,向sql里面设置参数
第五步,执行sql
第六步,释放资源
(6)代码
//使用工具类得到数据库的连接
conn = MyJdbcUtils.getConnection();
//编写sql
String sql = "select * from user where username=? and password=?";
//对sql进行预编译
psmt = conn.prepareStatement(sql);
//设置参数
psmt.setString(1, username);
psmt.setString(2, password);
//执行sql
rs = psmt.executeQuery();
if(rs.next()) {
System.out.println("login success");
} else {
System.out.println("fail");
}
七,使用PreparedStatement预编译对象实现crud的操作
注意地方:
第一,编写sql时候,参数使用?表示(占位符)
第二,预编译sql,设置参数的值,执行sql
1,使用PreparedStatement预编译对象实现查询的操作
(1)预编译对象executeQuery()执行查询语句
2,使用PreparedStatement预编译对象实现增加的操作
(1)预编译对象executeUpdate()执行增加的语句
3,使用PreparedStatement预编译对象实现修改的操作
(1)预编译对象executeUpdate()执行修改的语句
4,使用PreparedStatement预编译对象实现删除的操作
(1)预编译对象executeUpdate()执行删除的语句
经典综合案列:
(一)db.propreties 文件
drivername=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test01
usename=root
password=123
(二)MyJdbcUtils.java
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class MyJdbcUtils {
private static String drivername;
private static String url;
private static String usename;
private static String password;
static{
drivername=ResourceBundle.getBundle("db").getString("drivername");
url = ResourceBundle.getBundle("db").getString("url");
usename = ResourceBundle.getBundle("db").getString("usename");
password = ResourceBundle.getBundle("db").getString("password");
}
public static Connection getConnection() throws Exception{
Class.forName(drivername);
Connection conn=DriverManager.getConnection(url, usename, password);
return conn;
}
public static void getClose(Connection conn,Statement stmt,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
package cn.itcast;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import utils.MyJdbcUtils;
public class test {
public static void main(String[] args) throws Exception {
getSelect(1, "12345");
}
public static void getSelect(int id,String password) throws Exception{
Connection conn=null;
PreparedStatement psmt = null;
ResultSet rs=null;
try{
conn = MyJdbcUtils.getConnection();
String sql="select * from user where id=? and password=?";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
psmt.setString(2, password);
rs = psmt.executeQuery();
if(rs.next()){
System.out.println("success");
}else{
System.out.println("fail");
}
}catch(Exception e){
e.printStackTrace();
}finally{
MyJdbcUtils.getClose(conn, psmt, rs);
}
}
}
一,jdbc的简介:
java database connectivity java数据库的连接
二,使用jdbc对数据库进行(crud)操作是固定的(加创编执释)
1,加载数据库驱动
DriverManager里面的registerDriver
//导入的是mysql.jdbc.Driver的包,但是一般不用这个方法,因为它会加载驱动两次可以查API
一般用:Class.forName("com.mysql.jdbc.Driver");//反射的方式
2,创建与数据库的连接
DriverManager里面的getConnection(String url,String username,String password);
3,编写sql
4,执行sql
Statement里面的executeQuery(String sql);
5,释放资源(关闭连接)
案列:
//加载驱动
// DriverManager.registerDriver(new Driver());//导入的是mysql.jdbc.Driver的包,但是一般不用这个方法,因为它会加载驱动两次可以查API
// 一般用:Class.forName("com.mysql.jdbc.Driver");//反射的方式
public static void main(String[] args) throws Exception {
//加载驱动
DriverManager.registerDriver(new Driver());
//创建连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb2", "root", "root");
//编写sql
String sql = "select * from user";
//执行sql
//得到statement
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
//遍历结果集得到每条记录
while(rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
int chinese = rs.getInt("chinese");
int english = rs.getInt("english");
System.out.println(id+" :: "+username+" :: "+chinese+" :: "+english);
}
//释放资源
rs.close();
stmt.close();
conn.close();
}
三,jdbc的Statement对象
1,执行sql的对象,接口,在java.sql包里面
2,执行查询操作方法
ResultSet executeQuery(String sql) ,返回查询的结果集
3,执行增加 修改 删除的方法
int executeUpdate(String sql) ,返回成功的记录数
4,执行sql语句的方法
boolean execute(String sql) ,返回是布尔类型,如果执行的是查询的操作返回true,否则返回的false
5,执行批处理的方法
addBatch(String sql):把多个sql语句放到批处理里面
int[] executeBatch():执行批处理里面的所有的sql
四,使用jdbc进行crud操作
1,实现对数据库表记录进行查询的操作
executeQuery(sql)
2,实现对数据库表记录进行增加的操作
executeUpdate(sql)
3,实现对数据库表记录进行修改的操作
executeUpdate(sql)
4,实现对数据库表记录进行删除的操作
executeUpdate(sql)
五,jdbc工具类的封装
1)
第一种,使用properties类
(1)代码
//创建properties对象
Properties p = new Properties();
//文件的输入流
InputStream in = new FileInputStream("src/db.properties");
//把文件的输入流放到对象里面
p.load(in);
String drivername = p.getProperty("drivername");
String url = p.getProperty("url");
String username = p.getProperty("username");
String password = p.getProperty("password");
第二种,使用ResourceBundle类
* 使用范围:首先读取的文件的格式需要时properties,文件需要放到src下面
(1)代码
String drivername = ResourceBundle.getBundle("db").getString("drivername");
String url = ResourceBundle.getBundle("db").getString("url");
String username = ResourceBundle.getBundle("db").getString("username");
String password = ResourceBundle.getBundle("db").getString("password");
2)
代码
public static String drivername;
public static String url;
public static String username;
public static String password;
//在类加载时候,执行读取文件的操作
static {
drivername = ResourceBundle.getBundle("db").getString("drivername");
url = ResourceBundle.getBundle("db").getString("url");
username = ResourceBundle.getBundle("db").getString("username");
password = ResourceBundle.getBundle("db").getString("password");
}
六,sql的注入和防止
防止sql的注入
(1)使用PreparedStatement预编译对象防止sql注入
(2)创建PreparedStatement对象 prepareStatement(String sql)
(3)PreparedStatement接口的父接口Statement
(4)什么是预编译
(5)步骤:
第一步,加载驱动,创建数据库的连接
第二步,编写sql
第三步,需要对sql进行预编译
第四步,向sql里面设置参数
第五步,执行sql
第六步,释放资源
(6)代码
//使用工具类得到数据库的连接
conn = MyJdbcUtils.getConnection();
//编写sql
String sql = "select * from user where username=? and password=?";
//对sql进行预编译
psmt = conn.prepareStatement(sql);
//设置参数
psmt.setString(1, username);
psmt.setString(2, password);
//执行sql
rs = psmt.executeQuery();
if(rs.next()) {
System.out.println("login success");
} else {
System.out.println("fail");
}
七,使用PreparedStatement预编译对象实现crud的操作
注意地方:
第一,编写sql时候,参数使用?表示(占位符)
第二,预编译sql,设置参数的值,执行sql
1,使用PreparedStatement预编译对象实现查询的操作
(1)预编译对象executeQuery()执行查询语句
2,使用PreparedStatement预编译对象实现增加的操作
(1)预编译对象executeUpdate()执行增加的语句
3,使用PreparedStatement预编译对象实现修改的操作
(1)预编译对象executeUpdate()执行修改的语句
4,使用PreparedStatement预编译对象实现删除的操作
(1)预编译对象executeUpdate()执行删除的语句
经典综合案列:
(一)db.propreties 文件
drivername=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test01
usename=root
password=123
(二)MyJdbcUtils.java
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class MyJdbcUtils {
private static String drivername;
private static String url;
private static String usename;
private static String password;
static{
drivername=ResourceBundle.getBundle("db").getString("drivername");
url = ResourceBundle.getBundle("db").getString("url");
usename = ResourceBundle.getBundle("db").getString("usename");
password = ResourceBundle.getBundle("db").getString("password");
}
public static Connection getConnection() throws Exception{
Class.forName(drivername);
Connection conn=DriverManager.getConnection(url, usename, password);
return conn;
}
public static void getClose(Connection conn,Statement stmt,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
package cn.itcast;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import utils.MyJdbcUtils;
public class test {
public static void main(String[] args) throws Exception {
getSelect(1, "12345");
}
public static void getSelect(int id,String password) throws Exception{
Connection conn=null;
PreparedStatement psmt = null;
ResultSet rs=null;
try{
conn = MyJdbcUtils.getConnection();
String sql="select * from user where id=? and password=?";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
psmt.setString(2, password);
rs = psmt.executeQuery();
if(rs.next()){
System.out.println("success");
}else{
System.out.println("fail");
}
}catch(Exception e){
e.printStackTrace();
}finally{
MyJdbcUtils.getClose(conn, psmt, rs);
}
}
}