1.Driver接口
url组成
jdbc:子协议:子名称(数据库所在的主机IP):端口号/数据库名
jdbc:mysql://localhost(127.0.0.1):3306/test
/*
* Dirver接口:数据库厂商必须提供的接口,从其中获取数据库的连接
* 1.加入mysql驱动
* 1)解压mysql- connector-java-5.1.7-bin.jar
* 2)在当前项目下新建lib文件夹
* 3)把mysql-connector-java-5.1.7-bin.jar复制到lib中
* 4)右键build path add to buildpath加到类路径下
* */
@Test
public void testDriver() throws SQLException {
//1.创建Driver 实现类的对象
Driver driver = new com.mysql.jdbc.Driver();
//2.准备连接数据库的基本信息:url,user,password
String url = "jdbc:mysql://localhost:3306/db_person";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "1234");
//3.调用Driver接口的connect(url, info)获取数据库连接
Connection connection = driver.connect(url, info);
System.out.println(connection);
}
数据库与配置文件的解耦:将驱动Driver的信息放入一个配置文件中jdbc.properties中
/*
* 一个通用的方法,再不修改源程序的情况下,获取任何数据库的连接
* 解决方法:把数据库驱动Driver实现类的全类名、url、user、password
* 放入一个配置文件中,通过修改配置文件的方式实现和具体的数据库解耦
*
* */
public Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
//读取类路径下的jdbc.propertier文件
InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");//driver=com.mysql.jdbc.Driver
jdbcUrl = properties.getProperty("jdbcUrl");//jdbcUrl=jdbc:mysql://localhost:3306/db_person
user = properties.getProperty("user");//user=root
password = properties.getProperty("password");//password=1234
//通过反射创建Driver对象
Driver driver = (Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
//通过driver的connect方法获取数据库连接
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
@Test
public void tetGetConnection() throws Exception {
System.out.println(getConnection());
}
jdbc.properties中的内容
driver=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/db_person user=root password=1234
2.DriverManager类是驱动程序管理器类:管理驱动程序
/*
*DriverManager是驱动的管理类
* 1.可以通过重载的getConnection()方法获取数据库连接
* 2.可以同时管理多个驱动程序:若注册了多个数据库连接,
* 则调用getConnection()方法时传入的参数不同,即返回不同的数据库连接
*
*/
@Test
public void testDriverManager() throws Exception {
//1.准备连接数据库的4个字符串
//驱动的全类名
String driverClass = "com.mysql.jdbc.Driver";
//jdbc url
String jdbcUrl = "jdbc:mysql://localhost:3306/db_person";
//user password
String user = "root";
String password = "1234";
//2.加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块)
// DriverManager.registerDriver(Class.forName(driverClass).newInstance());
Class.forName(driverClass);
//3.通过DriverManager 的 getConnection()方法获取数据库连接
Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
System.out.println(connection);
}
3.通过Statement的CreatStatement()和executeUpdate(sql)来进行数据的插入、删除、修改
/*
* 通过JDBC向指定的数据表中插入、删除、修改一条记录
*
* 1.Statement:用于执行SQL语句的对象
* 1)通过Connection的CreatStatement()方法来获取
* 2)通过executeUpdate(sql)可以执行SQL语句
* 3)传入的SQL可以使INSERT、UPDATE、或DELETE,但不能是SELECT
*
* 2.Connection、Statement都是应用程序和数据库服务器的连接资源。使用后一定要关闭。
* 需要在finally中关闭该对象
*
* 3.关闭的顺序:从后向前
*
* */
@Test
public void testStatement() {
Connection conn = null;
Statement statement = null;
try {
//1.获取数据库连接
conn = getConnection();//上面的程序中有写此方法
//3.准备插入的SQL语句
String sql = "INSERT INTO student(id, name, sex, age) "
+ "VALUES(null, 'lili', 'woman', '20');";
String sql1 = "DELETE FROM student WHERE id = 1";
String sql2 = "UPDATE student SET name = 'Skye'"
+ " WHERE id = 2";
//4.执行插入
//1)获取操作SQL语句的Statement对象:调用Connection的CreatStatement()方法来获取
statement = (Statement) conn.createStatement();
//2)调用Statement对象的executeUpdate(sql)执行SQL语句进行插入
statement.executeUpdate(sql2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(statement != null) {
//5.关闭Statement对象
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
//2.关闭连接
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5.ResultSet:结果集,封装了使用JDBC进行查询的结果
/**
* ResultSet:结果集,封装了使用JDBC进行查询的结果
* 1.调用Statement对象的executeQuery(sql)可以得到结果集
* 2.ResultSet返回的实际上就是一张数据表,有一个指针指向数据表的第一行的前面
* 可以调用next()方法检测下一行是否有效。若有效该方法返回true,且指针下移,相当于
* Iterator对象的hasNext()和next()方法的合体
* 3.当指针对到一行时,可以通过getXxx(index)或getXxx(columnName)获取每一列的值
* 例如:getInt(1),getString("name");
* 4.ResultSet当然也需要关闭
*/
@Test
public void testResultSet() {
//获取id=4的student数据表的记录,并打印
Connection conn = null;
Statement statement = null;
ResultSet res = null;
try {
//1.获取Connection
conn = TestStatement.getConnection();
//2.获取Statement
statement = (Statement) conn.createStatement();
//3.准备SQL
String sql = "SELECT * FROM student";
//4.执行查询,得到ResultSet
res = statement.executeQuery(sql);
//5.处理结果集
//5.1调用next()方法检测下一行是否有效。若有效该方法返回true,且指针下移
//5.2通过getXxx(index)或getXxx(columnName)获取每一列的值
while(res.next()) {
int id = res.getInt(1);
String name = res.getString(2);
String sex = res.getString(3);
int age = res.getInt("age");
System.out.print(id);
System.out.print(name);
System.out.print(sex);
System.out.print(age);
System.out.println();
}
} catch(Exception e){
e.printStackTrace();
}finally {
//6.关闭数据库资源
TestStatement.release(res, statement, conn);
}
}