JDBC_Test

package com.phoenix.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.junit.Test;
import com.mysql.jdbc.Driver;

public class JDBCTest {
	
	/*
	 * ResultSet:结果集,封装了使用JDBC进行查询的结果
	 * 1.调用Statement对象的executeQuery(sql)可以得到结果集
	 * 2.ResultSet返回的实际上就是一张数据表
	 * 有一个指针指向数据表的第一行的前面,可以调用next()方法检测下一行是否释放。
	 * 若有效该方法返回true,且指针下移
	 * 3.当指针定位到一行时,可以通过调用getXxx(index)或getXxx(columnName)
	 * 获取每一列的值。例如:getInt(1)、getString("name")
	 * 4.ResultSet也需要进行关闭
	 */
	@Test
	public void testResultSet(){
		//获取customers数据表的记录,并打印
		Connection conn = null;
		Statement statement = null;
		ResultSet rs = null;
		
		try {
			//1.获取Connection数据库连接
			conn = JDBCTools.getConnection();
			System.out.println(conn);
			//2.调用Statement对象
			statement = conn.createStatement();
			System.out.println(statement);
			//3.准备SQL
			String sql = "select id,name,email,birth from customers";
			
			//4.执行查询,得到一个ResultSet
			rs = statement.executeQuery(sql);
			System.out.println(rs);
			//5.处理ResultSet
			while(rs.next()){
				int id = rs.getInt(1);
				String name = rs.getString("name");
				String email = rs.getString("email");
				Date birth = rs.getDate(4);
				
				System.out.println(id);
				System.out.println(name);
				System.out.println(email);
				System.out.println(birth);
				System.out.println();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		//6.关闭数据库资源
		JDBCTools.release(rs,statement,conn);
	}
	
	/*
	 * 通用的更新的方法:包括insert、update、delete
	 * 版本一
	 */
	public void update(String sql){
		Connection conn = null;
		Statement statement = null;
		
		try {
			conn = JDBCTools.getConnection();
			statement = conn.createStatement();
			statement.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCTools.release(statement,conn);
		}
	}
	
	/*
	 * 通过JDBC向指定的数据表中插入一条记录
	 * 
	 * 1.Statement:用于执行SQL语句的对象
	 * 	(1) 通过Connection的createStatement()方法来获取
	 * 	(2) 通过executeUpdate(sql)可以执行SQL语句
	 * 	(3) 传入的SQL可以是insert、update或delete,但不能是select
	 * 
	 * 2.Connection、Statement都是应用程序和数据库服务器的连接资源
	 * 	  使用后一定要关闭。需要在finally中关闭Connection和Statement对象
	 * 
	 * 3.关闭的顺序是:先关闭后获取,即先关闭Statement,后关闭Connection
	 * 
	 */
	@Test
	public void testStatement(){
		//1.获取数据库连接
		Connection conn = null;
		//3.执行插入
		//(1)获取操作SQL语句的Statement对象
		//		调用Connection的createStatement()方法来获取
		Statement statement = null;
		try {
			conn = getConnection2();
			//2.准备插入的SQL语句
//			String sql = "insert into customers (name,email,birth)" 
//					+ "values('CSQ','CuiShiQing@qq.com','1999-09-16')";
			String sql = "delete from customers where id=1";
			statement = conn.createStatement();
			//(2)调用Statement对象的executeUpdate(sql)执行SQL语句进行插入
			statement.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(statement != null){
				//4.关闭Statement对象
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			if(conn != null){
				//5.关闭连接
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}

	@Test
	public void testGetConnection2() throws Exception{
		System.out.println(getConnection2());
	}

	public Connection getConnection2() throws Exception{
		//1.准备连接数据库的4个字符串
		String driverClass = null;	// 驱动的全类名
		String jdbcUrl = null;		// JDBC URL
		String user = null;			// 用户名
		String password = null;		// 密码

		//(1)创建Properties对象
		Properties properties = new Properties();
		//(2)获取jdbc.properties对应的输入流
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
		//(3)加载输入流
		properties.load(in);
		//(4)具体获取4个字符串
		driverClass = properties.getProperty("driver");
		jdbcUrl = properties.getProperty("jdbcUrl");
		user = properties.getProperty("user");
		password = properties.getProperty("password");
		//2.加载数据库驱动程序
		Class.forName(driverClass);
		//3.通过DriverManager的getConnection()方法获取数据库连接
		Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
		return conn;
	}

	/*
	 * DriverManager是驱动的管理类
	 * 1.可以通过重载的getConnection()方法获取数据库连接,较为方便
	 * 2.可以同时管理多个驱动程序,若注册了多个数据库连接,
	 * 如果调用getConnection()方法时传入的参数不同,则返回不同的数据库
	 */
	@Test
	public void testDriverManager() throws Exception{
		//1.准备连接数据库的4个字符串
		String driverClass = null;	// 驱动的全类名
		String jdbcUrl = null;		// JDBC URL
		String user = null;			// 用户名
		String password = null;		// 密码

		//读取类路径下的jdbc.properties文件
		InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
		Properties properties = new Properties();
		properties.load(in);
		driverClass = properties.getProperty("driver");
		jdbcUrl = properties.getProperty("jdbcUrl");
		user = properties.getProperty("user");
		password = properties.getProperty("password");

		//2.加载数据库驱动程序(注册驱动)
		//		DriverManager.registerDriver((java.sql.Driver) Class.forName(driverClass).newInstance());
		Class.forName(driverClass);
		//3.通过DriverManager.getConnection()方法获取数据库连接
		Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
		System.out.println(conn);
	}


	@Test
	public void testGetConnection() throws Exception{
		System.out.println(getConnection());
	}

	/*
	 * 编写一个通用的方法,在不修改源程序的情况下,可以获取任何数据库的连接
	 * 解决方案:把数据库驱动Driver实现类的全类名、url、user、password
	 * 放入一个配置文件中,通过修改配置文件的方式,实现和具体的数据库解耦
	 */
	public Connection getConnection() throws Exception{
		String driverClass = null;
		String jdbcUrl = null;
		String user = null;
		String password = null;

		//读取类路径下的jdbc.properties文件
		InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
		Properties properties = new Properties();
		properties.load(in);
		driverClass = properties.getProperty("driver");
		jdbcUrl = properties.getProperty("jdbcUrl");
		user = properties.getProperty("user");
		password = properties.getProperty("password");

		//通过反射创建Driver对象
		Driver driver = (Driver) Class.forName(driverClass).newInstance();

		Properties info = new Properties();
		info.put("user", user);
		info.put("password", password);
		//通过Driver的connect方法获取数据库连接
		Connection conn = driver.connect(jdbcUrl, info);
		return conn;
	}

	/*
	 * Driver是一个接口,数据库厂商必须提供实现的接口
	 * 能从其中获取数据库连接
	 * 
	 * 1、加入MySQL驱动
	 * 	1.在当前项目下新建lib目录
	 * 	2.把mysql-connector-java-commercial-5.1.25-bin.jar复制到lib目录下
	 * 	3.右键jar包 -> Build Path -> Add to Build Path加入到类路径下
	 * 
	 * 
	 */
	@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/ycy";	//127.0.0.1可以替换localhost
		Properties info = new Properties();
		info.put("user", "root");
		info.put("password","0722");
		//3.调用Driver接口的connect(url,info)方法获取数据库连接
		Connection conn = driver.connect(url, info);
		System.out.println(conn);

	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值