jdbc基本使用

1. jdbc工具类,提供数据库连接和释放

import java.io.InputStream;
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 {

	public static void release(ResultSet rs, Statement statement, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * 关闭 Statement 和 Connection
	 * 
	 * @param statement
	 * @param conn
	 */
	public static void release(Statement statement, Connection conn) {
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * 1. 获取连接的方法. 通过读取配置文件从数据库服务器获取一个连接.
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception {
		// 1. 准备连接数据库的 4 个字符串.
		// 1). 创建 Properties 对象
		Properties properties = new Properties();

		// 2). 获取 jdbc.properties 对应的输入流
		InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");

		// 3). 加载 2) 对应的输入流
		properties.load(in);

		// 4). 具体决定 user, password 等4 个字符串.
		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String jdbcUrl = properties.getProperty("jdbcUrl");
		String driver = properties.getProperty("driver");

		// 2. 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
		Class.forName(driver);

		// 3. 通过 DriverManager 的 getConnection() 方法获取数据库连接.
		return DriverManager.getConnection(jdbcUrl, user, password);
	}
}

2. 使用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 的 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(3);
			Date birth = rs.getDate(4);

			System.out.println(id);
			System.out.println(name);
			System.out.println(email);
			System.out.println(birth);
		}

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		// 6. 关闭数据库资源.
		JDBCTools.release(rs, statement, conn);
	}

}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值