JDBC是什么?
是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
数据库驱动是什么?
数据库驱动是各大数据库厂商在遵循JDBC规范的基础上,为访问自己数据库开发的API。
连接数据库的前期准备?
URL(jdbc:mysql://localhost:3306/mydb),USERNAME,PASSWORD,DRIVER(com.mysql.jdbc.Driver)
JDBC中常用的API:
1、 driver接口 :
概念:数据库驱动程序的接口,所有具体数据库厂商需要的驱动程序需要实现该接口
2、DriverManager接口:
概念:驱动管理器的一个类,使用其注册驱动并获取Connection对象
3、Connection接口:
概念:具体连接数据库的对象
4、statement接口:
概念:用于执行静态 SQL 语句的语句接口
5、preparedStattement接口:
概念:用于执行预编译的 SQL 语句,是Statement的子接口
6、CallableStatement接口:
概念:用于执行 SQL 存储过程的接口,是PreparedStatement的子接口
7、ResultSet接口:
概念:结果集对象。 存储所有数据库查询的结果,用该对象进行数据遍历。
使用JDBC连接数据库的步骤:
第一种方式:(常用)
1.加载properties文件,获取properties文件中存放url,username,password,driver信息
InputStream in = new FileInputStream("db.properties"); //文件以流的形式获取到
Properties pro = new Properties();//创建properties对象
pro.load(in);//加载文件流并将信息存取到pro对象
String url = pro.getProperty("url");
String name = pro.getProperty("username");
String password = pro.getProperty("passwrod");
String driver = pro.getProperty("driver");
2.加载驱动类,通过DriverManager获取connection对象
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,name,password);
第二种方式:
( url,username,password,drive 信息与第一种方式一致)
1.直接创建driver对象
Driver driver = new Driver();
2.注册一个驱动
DriverManager.registerDriver(driver);
3.获取connection对象
Connection conn = DriverManager.getConnection(url,name,password);
第三种方式:(最古老的一种方式)
( url,username,password,drive 信息与第一种方式一致)
1.直接创建driver对象
Driver driver = new Driver();
2.直接调用driver接口连接数据库的方法
Connection connection = dirver.connect(url, info);
为什么选择第一种方式?
因为反射是运行时根据全类名动态生成的Class对象,完全可以把这个全类名写在xml或者properties中去,
不仅从代码上解耦和,而且需要更换数据库时,不需要进行代码的重新编译。
**实体类的编写:**
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/*
* jdbc操作
*/
public class JdbcUtils {
private static String url = null;
private static String user = null;
private static String password = null;
private static String driver = null;
private static String statement = null;
//类加载时就会创建connection对象
static {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("db.properties");
Properties pro = new Properties();
pro.load(fileInputStream);
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
Class.forName(driver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 获取connection对象
*/
public static Connection getConnection()throws Exception{
Connection con = DriverManager.getConnection(url,user,password);
System.out.println("con==="+con);
return con;
}
/*
* 释放资源
*/
public static void close(Statement statement,ResultSet rs,Connection conn) {
closeStatement(statement);
closeResultSet(rs);
closeConnection(conn);
}
public static void closeStatement(Statement statement) {
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeResultSet(ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeConnection(Connection conn) {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}