1.连接数据库必要的步骤:
jdbc:
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/lxn
user=root
password=lxn123
oracle:
#driver=oracle.jdbc.driver.OricerDriver
#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
#user=system
#password=lxn123
2.连接数据库的两种方法,实现数据库的增删改查
package com.lanqiao.javatest;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Properties;
import org.junit.Test;
import com.mysql.jdbc.Statement;
public class TestDriverManager {
	/*
	 * 连接数据库的方法:
	 * 1.使用DriverManager连接数据库
	 * 2.Driver连接:是一个接口,是一个本地实现的接口,能从其中获取数据库的驱动的链接
	 * */
	
	//方法一:DriverManager方法,连接数据库
	
	public void testDriverManager() 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);//从输入流中读取文件
		
		//依次获取properties中的文件
		driverClass=properties.getProperty("driver");
		jdbcUrl=properties.getProperty("jdbcUrl");
		user=properties.getProperty("user");
		password=properties.getProperty("password");
	
		Class.forName(driverClass);//反射机制获取文件中的driverClass
		
		//通过driverManager中的getConnection方法获取数据的连接
		Connection conn=DriverManager.getConnection(jdbcUrl,user,password);
		System.out.println(conn);
	}
	
	//方法二:使用driver接口连接数据库
	//1.
	public Connection getConnection() throws Exception{
		//对数据库的连接使用者个,里面有Connection接口,实现数据的连接,并可以进行数据库的增删改查的操作
		//连接数据里的四个必要步骤:
		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");
		
		//反射机制获取
		@SuppressWarnings("unused")
		Driver driver=(Driver)Class.forName(driverClass).newInstance();//获取运行时类的方法及属性
		
		Properties info=new Properties();
		info.put("user", "root");
		info.put("password", "lxn123");
		Connection conn=driver.connect(jdbcUrl, info);
		return conn;
	}
	
	//测试testDriver()方法;
	public void testDriver1() throws Exception{
		System.out.println(getConnection());
	}
	/*
	 * 1.连接数据库之后对指定的数据库中的增加,删除,更改,查询的操作
	 * statement用于执行sql语句的对象;
	 * 通过Connection的stateStatement()方法来获取
	 * 同过executeUpdate(sql)可以执行sql语句
	 * 传入的sql语句可以是insect(插入),update(修改),delete(删除),但是不可以是select(查询)
	 * 2.需要注意的是:Connection,statement都是应用程序和数据库服务器的链接资源,使用后都要关闭;
	 * */
	public void testStatement() throws Exception{//数据库的插入,删除,更改,查询需要的步骤;
		//1.获取数据库的
		Connection connn=getConnection();
		//2.准备插入的sql语句
		
		//将sql语句插入到在数据库中建立的表格中
		//4.进行插入,删除,更改,查询的操作
		
		//插入
		String sql1="INSERT INTO CUSTOMER(NAME,EMALL,BIRTH)VALUES(‘pa‘,‘LJ‘,‘1932-2-12‘);";
		//删除
		String sql2="delete from customer where id=27";
		//修改
		String sql3="update customer set name=‘jiajia‘ where id=2564";
		//获取sql语句的statement对象,调用statement对象的executeUpdate(sql),执行sql语句的插入
		Statement statement=(Statement) connn.createStatement();
		statement.executeUpdate(sql1);
		statement.executeUpdate(sql2);
		statement.executeUpdate(sql3);
		//5关闭statement对象。
		statement.close();
		//3.关闭连接
			connn.close();
	}
	//sql为数据库语句,这个为数据库的增删改的方法
	public void testStatement1(String sql){
		Connection conn=null;
		Statement sta=null;
		try {
			conn=getConnection();
			sta=(Statement) conn.createStatement();
			sta.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(sta!=null){
				try {
					sta.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	//数据库的查询方法
	@Test
	public void testResultSet(){
		//获取指定的id,或name的属性记录值,并打印
		Connection connection=null;
		Statement statement=null;
		ResultSet resultset=null;
		
		try {
			//1.获取Connection
			connection=getConnection();
		
			//2.获取Statement
			statement=(Statement)connection.createStatement();
			
			//3.准备sql语句
			String sql="select id,name,email,birth from customer";
		
			//4.执行查询,得到ResultSet
			resultset=statement.executeQuery(sql);
			
			//5.处置ResultSet
			while(resultset.next()){
				int id=resultset.getInt(1);
				String name=resultset.getString(2);
				String email=resultset.getString(3);
				Date birth=resultset.getDate(4);
				System.out.println(id+"--"+name+"--"+email+"--"+birth);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		//6.关闭数据库资源
		finally{
			release(connection,statement,resultset);
		}
	}
	
	//关闭数据库资源的方法
	public static void release(Connection conn,Statement sta,ResultSet rs){
		if(conn!=null){
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(sta!=null){
			try {
				sta.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(rs!=null){
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
原文:http://www.cnblogs.com/lxnlxn/p/5766189.html