当前位置: 移动技术网 > IT编程>开发语言>Java > JDBC数据库基本操作

JDBC数据库基本操作

2018年12月04日  | 移动技术网IT编程  | 我要评论

1.什么是jdbc?

在看jdbc的概念之前先来看看什么是数据库驱动。

数据库驱动中驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡,同样道理,我们安装好数据库之后,我们的应用程序也是不能直接使用数据库的,必须要通过相应的数据库驱动程序,通过驱动程序去和数据库打交道。

sun公司为了简化、统一对数据库的操作,定义了一套java操作数据库的规范(接口),称之为jdbc(java data base connectivity)。这套接口由数据库厂商去实现,这样,开发人员只需要学习jdbc接口,并通过jdbc加载具体的驱动,就可以操作数据库。

综上,jdbc是一个独立于特定数据库管理系统、通用的sql数据库存取和操作的公共接口,定义了用来访问数据库的标准java类库,使用这个类库可以以一种标准的方法方便地访问数据库资源。jdbc的目标是使程序员使用jdbc可以连接任何提供了jdbc驱动程序的数据库系统,这样使得程序员无需对特定的数据库系统的特点有过多的了解,从而大大简化和加快了开发过程。

2.jdbc api

jdbc api是一系列的接口,它使得应用程序能够进行数据库连接,执行sql语句,并且得到返回结果。数据库厂商使用的java.sql.driver接口是所有jdbc驱动程序需要实现的接口,在java程序中不需要直接去访问实现了driver接口的类,而是由驱动程序管理器类java.sql.drivermanager去调用这些driver实现。

3.jdbc获取数据库的连接

3.1 使用driver接口获取数据库的连接

package com.test.jdbc;

import java.io.inputstream;
import java.sql.connection;
import java.sql.driver;
import java.util.properties;

import org.junit.test;
/*
 * 编写一个通用的方法,在不修改源程序的条件下,可以获取任何数据库的连接
 * */

public class jdbctest {	
	public connection getconnection() throws exception{
//准备连接数据库的4个字符串 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); //读取properties文件内容 driverclass=properties.getproperty("driver"); jdbcurl=properties.getproperty("jdbcurl"); user=properties.getproperty("user"); password=properties.getproperty("password"); //通过反射创建java对象 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 testgetconnection() throws exception{ system.out.println(getconnection()); } }

 jdbc.properties

driver=com.mysql.jdbc.driver
jdbcurl=jdbc:mysql://localhost:3303/extra
user=root
password=0404

3.2 使用drivermanager类获取数据库连接

通过drivermanager连接数据库的基本步骤分为:

①准备连接数据库的4个字符串,driverclass,jdbcurl,user,password;

1).获取类路径下的jdbc.properties文件

2).读取properties文件内容,获取4个字符串的值

②加载数据库驱动程序;

③通过drivermanager的getconnection()方法获取数据库连接;

package com.test.jdbc;

import java.io.inputstream;
import java.sql.connection;
import java.sql.driver;
import java.sql.drivermanager;
import java.sql.sqlexception;
import java.util.properties;
import org.junit.test;

public class jdbctest {    
    
    public connection getconnection() throws exception{
        //1.准备连接数据库的4个字符串
        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);
        //读取properties文件内容
        driverclass=properties.getproperty("driver");
        jdbcurl=properties.getproperty("jdbcurl");
        user=properties.getproperty("user");
        password=properties.getproperty("password");
        //2.加载数据库驱动程序
        class.forname(driverclass);
        //3.通过drivermanager的getconnection()方法获取数据库连接
        connection connection=drivermanager.getconnection(jdbcurl,user,password);
        return connection;
    }
    @test
    public void testgetconnection() throws exception{
        system.out.println(getconnection());
    }
}

使用drivermanager可以注册多个驱动程序,从而使得使用多个jdbcurl可以连接不同的数据库。

4.通过statement执行更新操作

statement是用于执行sql语句的对象:

①通过connection的createstament()方法来获取;

②通过executeupdate(sql)可以执行sql语句;

③传入的sql可以是insert,update或delete,但不能是select;

④关闭的顺序是先关闭后获取的,即先关闭statement,再关闭connection;

package com.test.jdbc;

import java.io.inputstream;
import java.sql.connection;
import java.sql.driver;
import java.sql.drivermanager;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.properties;
import org.junit.test;

/**
 * @author administrator
 *
 */
public class jdbctest {	
	@test
	public void teststatement() throws exception{
		connection con=null;
		statement statement=null;
		try{
			//1.获取数据库连接
			con=getconnection();
			//2.准备插入的sql连接
			string sql="insert into test values(null,'b','bdbs.@koala.com','2018-8-09')";
			//3.执行插入
			//1).获取操作sql语句的statement对象,调用connection的createstatement()方法来获取;
			statement=con.createstatement();
			//2).调用statement对象的executeupdate(sql)执行sql语句进行插入
			statement.executeupdate(sql);
		}catch(exception e){
			e.printstacktrace();
			}finally{
				//使用try...catch...是为了确保出现异常也能关闭数据库。
				//4.关闭statement对象
				try {
					if(statement!=null)
					statement.close();
				} catch (sqlexception e) {
					e.printstacktrace();
				}finally{
					//5.关闭数据库连接
					if(con!=null)
			        con.close();
				}
		}
	}
	public connection getconnection() throws exception{
		//1.准备连接数据库的4个字符串
		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);
		//读取properties文件内容
		driverclass=properties.getproperty("driver");
		jdbcurl=properties.getproperty("jdbcurl");
		user=properties.getproperty("user");
		password=properties.getproperty("password");
		//2.加载数据库驱动程序
		class.forname(driverclass);
		//3.通过drivermanager的getconnection()方法获取数据库连接
		connection connection=drivermanager.getconnection(jdbcurl,user,password);
		return connection;
	}
	@test
	public void testgetconnection() throws exception{
		system.out.println(getconnection());
	}
}

5.一个通用的更新数据库的方法,包括insert,update,delete。

首先将数据库的连接和释放的方法封装到工具类中:

package com.test.jdbc;

import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.properties;
import org.junit.test;
/*
 * 操作jdbc的工具类,其中封装了一些工具方法。
 */
public class jdbctools {
	//获取连接的方法
	public static connection getconnection() throws exception{
		//1.准备连接数据库的4个字符串
		string driverclass=null;
		string jdbcurl=null;
		string user=null;
		string password=null;
		//获取类路径下的jdbc.properties文件
		inputstream in=jdbctools.class.getresourceasstream("jdbc.properties");
		properties properties=new properties();
		properties.load(in);
		//读取properties文件内容
		driverclass=properties.getproperty("driver");
		jdbcurl=properties.getproperty("jdbcurl");
		user=properties.getproperty("user");
		password=properties.getproperty("password");
		//2.加载数据库驱动程序
		class.forname(driverclass);
		//3.通过drivermanager的getconnection()方法获取数据库连接
		connection connection=drivermanager.getconnection(jdbcurl,user,password);
		return connection;
	}
	@test
	public void testgetconnection() throws exception{
		system.out.println(getconnection());
	}
	//释放连接的方法
	public static void release(statement statement,connection connection){
		//使用try...catch...是为了确保出现异常也能关闭数据库。
		//4.关闭statement对象
		if(statement!=null){
		try {
			statement.close();
		} catch (sqlexception e) {
			e.printstacktrace();
		}
		}
		if(connection!=null){
		try {
			//5.关闭数据库连接
			connection.close();
		} catch (sqlexception e) {
			e.printstacktrace();
		}
	}
	}
}

通用的更新方法,包括insert,update,delete:

package com.test.jdbc;

import java.sql.connection;
import java.sql.statement;
import org.junit.test;
import com.test.jdbc.jdbctools;

public class jdbctest {	
	public void update(string sql){
		connection con=null;
		statement statement=null;
		try{
			con=jdbctools.getconnection();
			statement=con.createstatement();
			statement.executeupdate(sql);
		}catch(exception e){
			e.printstacktrace();
		}finally{
			jdbctools.release(statement, con);
		}
	}
}

6.通过resultset执行查询操作

resultset结果集,封装了使用jdbc进行查询的结果。

①调用statement对象的executequery(sql)可以得到结果集;

②resultset返回的实际上是一张数据表,有一个指针指向数据表的第一行的前面。可以调用next()方法检测下一行是否有效,若有效该方法返回true,且指针下移,相当于iterator对象的hasnext()和next()方法的结合体;

③当指针对位到一行时,可以通过getxxx(index)或getxxx(columnname)获取每一列的值,例如:getint(1),getstring("name");

④resultset也需要关闭。

数据表如下,获取所有信息并打印。

 

package com.test.jdbc;

import java.sql.connection;
import java.sql.date;
import java.sql.resultset;
import java.sql.statement;
import org.junit.test;
import com.test.jdbc.jdbctools;

public class jdbctest {	
	@test
	public void testresultset() throws exception{
		//1.获取connection
		connection con=null;
		statement statement=null;
		resultset resultset=null;
		try{
			jdbctools tool=new jdbctools();
			con=tool.getconnection();
			//2.获取statement
			statement=con.createstatement();
			//3.准备sql
			string sql="select id,name,email,birth from test";
			//4.执行查询,得到resultset
			resultset=statement.executequery(sql);
			//5.处理resultset
			while(resultset.next()){
				int id=integer.parseint(resultset.getstring(1));
				string name=resultset.getstring("name");
				string email=resultset.getstring(3);
				date date=resultset.getdate(4);
				
				system.out.println(id);
				system.out.println(name);
				system.out.println(email);
				system.out.println(date);
			}
		}catch(exception e){
			e.printstacktrace();
		}finally{
			//6.关闭数据库资源
			jdbctools.release(statement,con,resultset);
		}
	}
}
	

运行结果:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网