JDBC(Java Database Connectivity)是一个独立与特定数据库管理系统、通用的SQL数据库存取和操做的公共接口(一组API),定义了用来访问数据库的标准Java类库。java
JDBC接口(API)包括两个层次:mysql
JDBC API 是一系列的接口,它使得应用程序可以进行数据库联接,执行SQL语句,而且获得返回结果。sql
1. Driver 接口数据库
2. 创建链接oracle
能够调用 DriverManager 类的 getConnection() 方法创建到数据库的链接函数
3. JDBC URLsqlserver
JDBC URL 用于标识一个被注册的驱动程序,驱动程序管理器经过这个 URL 选择正确的驱动程序,从而创建到数据库的链接。学习
4. 几种经常使用数据库的JDBC URLthis
格式:url
对于 Oracle 数据库链接,采用以下形式:
对于 SQLServer 数据库链接,采用以下形式:
对于 MYSQL 数据库链接,采用以下形式:
/** * Driver 是一个接口:数据库厂商必需提供实现的接口 * @throws SQLException */ @Test public void test1() throws SQLException { //1.建立一个Driver类的对象 Driver driver = new com.mysql.jdbc.Driver(); //2.链接数据库的基本信息:url、user、password String url = "jdbc:mysql://localhost:3306/jdbctest"; Properties info = new Properties(); info.put("user", "root"); info.put("password", "12345"); //3.调用Driver接口的connect获取数据库链接 Connection connection = driver.connect(url, info); System.out.println(connection); }
方法改进----通用方法获取数据库链接
把数据库驱动 Driver实现类的全类名、url、user、password放入一个配置文件中,经过修改配置文件的方式来实现数据库链接。
/** * 写一个方法,在不修改源程序的状况下,能够获取任何数据库的链接 * 解决方案:把数据库驱动 Driver实现类的全类名、url、user、password放入 * 一个配置文件中,经过修改配置文件的方式来实现数据库链接 */ public Connection getConnection() throws Exception { String driverClass = null; String jdbcUrl = null; String user = null; String password = null; //读取类路径下的 jdbc.properties 文件 java.io.InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(in); driverClass = properties.getProperty("driver"); user = properties.getProperty("user"); password = properties.getProperty("password"); jdbcUrl = properties.getProperty("jdbcUrl"); //经过反射建立Driver对象 Driver driver = (Driver) Class.forName(driverClass).newInstance(); Properties info = new Properties(); info.put("user", user); info.put("password", password); Connection connection = driver.connect(jdbcUrl,info); return connection; } @Test public void test2() throws Exception { System.out.println(getConnection()); } //配置文件:jdbc.properties //位于类路径下 driver=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/jdbctest user=root password=12345
/** * DriverManager 是驱动的管理类 * 1.能够经过重载的 getConnection方法获取数据库的链接 * 2.能够同时管理多个驱动程序:若注册了多个数据库链接,则调用getConnection() * 方法时传入的参数不一样,即返回不一样的数据库链接 * @throws Exception */ @Test public void test3() throws Exception { //1.准备链接数据库的4个字符串 //驱动的全类名 String driverClass = "com.mysql.jdbc.Driver"; //JDBC URL String jdbcUrl = "jdbc:mysql://localhost:3306/jdbctest"; //用户名 String user = "root"; //密码 String password = "620422@MS"; //2.加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块) //DriverManager.registerDriver((Driver) Class.forName(driverClass).newInstance()); Class.forName(driverClass); //3.经过DriverManager的getConnection方法获取数据库链接 Connection connection = DriverManager.getConnection(jdbcUrl, user, password); System.out.println(connection); }
方法改进----通用方法
public Connection getConnection2() throws Exception { //1.准备链接数据库的4个字符串 //1.1 建立Properties对象 Properties properties = new Properties(); //1.2 获取jdbc.properties对应的输入流 java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); //1.3 加载文件 properties.load(in); //1.4 给字符串赋值 String driver = properties.getProperty("driver"); String jdbcUrl = properties.getProperty("jdbcUrl"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); //2.加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块) Class.forName(driver); //3.经过DriverManager的getConnection方法获取数据库链接 return DriverManager.getConnection(jdbcUrl, user, password); } @Test public void test4() throws Exception { System.out.println(getConnection2()); } //配置文件:jdbc.properties driver=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/jdbctest user=root password=12345
JDBC学习笔记:
1. 获取数据库链接 ----当前----
2. 经过Statement执行更新、查询操做 http://my.oschina.net/daowuming/blog/704384
3. 使用PrepareStatement http://my.oschina.net/daowuming/blog/704432
4. 使用ResultSetMetaData 对象处理结果集元数据 http://my.oschina.net/daowuming/blog/704487
5. 使用DatabaseMetaData获取数据库信息 http://my.oschina.net/daowuming/blog/704553
6. BLOB http://my.oschina.net/daowuming/blog/704593
7. 处理事务与隔离级别 http://my.oschina.net/daowuming/blog/704611
8. 批量处理 http://my.oschina.net/daowuming/blog/704641
9. 数据库链接池 http://my.oschina.net/daowuming/blog/704700
10. 调用函数与存储过程 http://my.oschina.net/daowuming/blog/704813