0.准备:
(1)从
http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html下载Oracle 11gR2的数据库驱动程序:
ojdbc6.jar
下载完成后,将其放到JDK的jre\lib\ext下,如D:\dev\Java\jdk1.7.0_21\jre\lib\ext
(2)启动oracle服务器端的监听器和Oracle服务
启动监听器:
$ lsnrctl start
启动Oracle服务:
$ sqlplus /nolog
SQL> conn / as sysdba
SQL> startup
1.简单形式:
ConnOracle.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnOracle{
public static void main(String[] args){
Connection conn = null;
Statement smt = null;
ResultSet rs = null;
try{
//1.加载数据库驱动
//Class.forName("oracle.jdbc.driver.OracleDriver");//会抛出ClassNotFoundException
Class.forName("oracle.jdbc.OracleDriver");//会抛出ClassNotFoundException
//2.使用DriverManager获取数据库连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.180:1521:orcl", "scott", "tiger");//会抛出SQLException
System.out.println("数据库连接成功: "+conn);
//3.使用Connection来创建一个Statement对象
smt = conn.createStatement();//会抛出SQLException
//4.使用Statement对象执行SQL语句
/*
Statement有三种执行sql语句的方法:
1. execute可执行任何SQL语句--返回一个boolean值
如果执行后的第一个结果是ResultSet,则返回true,否则返回false
2. executeQuery 执行Select语句--返回查询到的结果集
3. executeUpdate 用于执行DML语句和DDL语句--返回一个整数,执行DML代表被SQL语句影响的记录条数;执行DDL语句返回0
*/
rs = smt.executeQuery("select * from emp");//会抛出SQLException
//5.操作结果集
/*
ResultSet对象有两类方法操作查询结果集
1. next()将记录指针下移一行,first(),last()等
2. getXxx(列索引|列名)获取有记录指针指向行,特定列的值
*/
while(rs.next()){//会抛出SQLException
System.out.println(rs.getInt(1) + "\t"
+ rs.getString(2)+"\t"
+ rs.getString(3));
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
finally{
try{
//6.回收数据库资源
if(rs != null){
//关闭ResultSet
rs.close();//会抛出SQLException
}
if(smt != null){
//关闭Statement
smt.close();
}
if(conn != null){
//关闭Connection
conn.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}
}
}
运行结果:
2.抽象成一个类
(1)DBConn.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConn{
private Connection conn = null;
private Statement smt = null;
private ResultSet rs = null;
//private static String DB_CLASS_NAME = "oracle.jdbc.driver.OracleDriver";
private static String DB_CLASS_NAME = "oracle.jdbc.OracleDriver"; //数据库驱动类的字符串
private static String DB_URL = "jdbc:oracle:thin:@192.168.1.180:1521:orcl"; //数据库URL
private static String USERNAME = "scott"; //登陆数据库的用户名
private static String PASSWORD = "tiger"; //密码
/* 连接数据库 */
private void getConn(){
try{
//1 加载数据库驱动
Class.forName(DB_CLASS_NAME);
//2 使用DriverManager获取数据库连接
conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
if(conn == null){
System.err.println("警告:DriverManager.getConnection()获得数据库连接失败.\r\n\r\n连接类型:"
+ DB_CLASS_NAME + "\r\n连接位置:" + DB_URL);
}
System.out.println("数据库连接成功: "+conn);
}
/* 执行查询语句 */
public ResultSet executeQuery(String sql){
getConn();
System.out.println("SQL:> "+sql);//测试使用,打印即将执行的SQL语句
try{
//3 使用Connection对象创建Statement对象
smt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//4 使用Statement对象执行SQL语句
rs = smt.executeQuery(sql);
}
catch(SQLException e){
e.printStackTrace();
}
return rs;
}
/* 执行DDL和DML语句 */
public int executeUpdate(String sql){
int result = 0;
getConn();
System.out.println("SQL:> "+sql);//测试使用,打印即将执行的SQL语句
try{
//3 使用Connection对象创建Statement对象
smt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//4 使用Statement对象执行SQL语句
result = smt.executeUpdate(sql);
}
catch(SQLException e){
e.printStackTrace();
}
return result;
}
/* 关闭数据库连接 */
public void close(){
if(rs != null){
//关闭ResultSet
try{
rs.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
if(smt != null){
//关闭Statement
try{
smt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
//关闭Connection
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
System.out.println("数据库连接已关闭: "+conn);
}
}
(2)DBConn_Demo.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConn_Demo{
public static void main(String[] args){
/* 1 执行查询操作 */
DBConn db1 = new DBConn();
String sql1 = "select * from emp";//字符串型的sql语句末尾不要带分号
//执行SQL语句,返回结果集
ResultSet rs1 = db1.executeQuery(sql1);
//操作结果集
try{
while(rs1.next()){
System.out.println(rs1.getInt(1) + "\t"
+ rs1.getString(2)+"\t"
+ rs1.getString(3));
}
}
catch(SQLException e){
e.printStackTrace();
}
db1.close();
/* 2 执行DDL操作 */
DBConn db2 = new DBConn();
String sql2 = "create table demo(id int, name varchar(10), age int)";
int result1 = db2.executeUpdate(sql2);
System.out.println("有" + result1 + "条记录受到影响!");
db2.close();
/* 3 执行插入操作 */
DBConn db3 = new DBConn();
String sql3 = "insert into demo values(1, 'huhu', 18)";
int result2 = db3.executeUpdate(sql3);
System.out.println("有" + result2 + "条记录受到影响!");
db3.close();
/* 4 执行查询操作 */
DBConn db4 = new DBConn();
String sql4 = "select * from demo";//字符串型的sql语句末尾不要带分号
//执行SQL语句,返回结果集
ResultSet rs2 = db4.executeQuery(sql4);
//操作结果集
try{
while(rs2.next()){
System.out.println(rs2.getInt(1) + "\t"
+ rs2.getString(2)+"\t"
+ rs2.getString(3));
}
}
catch(SQLException e){
e.printStackTrace();
}
db4.close();
/* 5 执行删除操作 */
DBConn db5 = new DBConn();
String sql5 = "delete from demo";
int result3 = db5.executeUpdate(sql5);
System.out.println("有" + result3 + "条记录受到影响!");
db5.close();
/* 6 执行查询操作 */
DBConn db6 = new DBConn();
String sql6 = "select * from demo";//字符串型的sql语句末尾不要带分号
//执行SQL语句,返回结果集
ResultSet rs3 = db6.executeQuery(sql6);
//操作结果集
try{
while(rs3.next()){
System.out.println(rs3.getInt(1) + "\t"
+ rs3.getString(2)+"\t"
+ rs3.getString(3));
}
}
catch(SQLException e){
e.printStackTrace();
}
db6.close();
/* 7 执行DDL操作 */
DBConn db7 = new DBConn();
String sql7 = "drop table demo";
int result = db7.executeUpdate(sql7);
System.out.println("有" + result + "条记录受到影响!");
db7.close();
}
}
运行结果:
3.将数据库连接信息保存到一个dbconfig.properties配置文件中
这样的做法好处是:当我们需要把应用程序从开发环境移植到生产环境时,无需修改源代码,只需要修改dbconfig.properties配置文件即可。
(1)dbconfig.properties:
#DB_CLASS_NAME(数据库驱动类的类名)
DB_CLASS_NAME=oracle.jdbc.OracleDriver
#DB_CLASS_NAME=com.mysql.jdbc.Driver
#DB_URL(要连接数据库的地址)
DB_URL=jdbc:oracle:thin:@192.168.1.180:1521:orcl
#DB_URL=jdbc:mysql://127.0.0.1:3306/test
#USERNAME(数据库用户)
USERNAME=scott
#USERNAME=root
#PASSWORD(密码)
PASSWORD=tiger
#PASSWORD=
(2)DBConn1.java:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.FileInputStream;
import java.util.Properties;
public class DBConn1{
private Connection conn = null;
private Statement smt = null;
private ResultSet rs = null;
//private static String DB_CLASS_NAME = "oracle.jdbc.driver.OracleDriver";
private static String DB_CLASS_NAME = "oracle.jdbc.OracleDriver"; //数据库驱动类的字符串
private static String DB_URL = "jdbc:oracle:thin:@192.168.1.180:1521:orcl"; //数据库URL
private static String USERNAME = "scott"; //登陆数据库的用户名
private static String PASSWORD = "tiger"; //密码
private static String propFileName = "dbconfig.properties"; //指定资源文件保存的位置
private static Properties props = new Properties();
public DBConn1(){
try{
//使用Properties类来加载属性文件
FileInputStream in=new FileInputStream(propFileName);
props.load(in);
DB_CLASS_NAME = props.getProperty("DB_CLASS_NAME");
DB_URL = props.getProperty("DB_URL");
USERNAME = props.getProperty("USERNAME");
PASSWORD = props.getProperty("PASSWORD");
}
catch (Exception e) {
e.printStackTrace();
}
}
/* 连接数据库 */
private void getConn(){
try{
//1 加载数据库驱动
Class.forName(DB_CLASS_NAME);
//2 使用DriverManager获取数据库连接
conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
if(conn == null){
System.err.println("警告:DriverManager.getConnection()获得数据库连接失败.\r\n\r\n连接类型:"
+ DB_CLASS_NAME + "\r\n连接位置:" + DB_URL);
}
//System.out.println("数据库连接成功: "+conn);
}
/* 执行查询语句 */
public ResultSet executeQuery(String sql){
getConn();
System.out.println("SQL:> "+sql);//测试使用,打印即将执行的SQL语句
try{
//3 使用Connection对象创建Statement对象
smt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//4 使用Statement对象执行SQL语句
rs = smt.executeQuery(sql);
}
catch(SQLException e){
e.printStackTrace();
}
return rs;
}
/* 执行DDL和DML语句 */
public int executeUpdate(String sql){
int result = 0;
getConn();
System.out.println("SQL:> "+sql);//测试使用,打印即将执行的SQL语句
try{
//3 使用Connection对象创建Statement对象
smt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//4 使用Statement对象执行SQL语句
result = smt.executeUpdate(sql);
}
catch(SQLException e){
e.printStackTrace();
}
return result;
}
/* 关闭数据库连接 */
public void close(){
if(rs != null){
//关闭ResultSet
try{
rs.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
if(smt != null){
//关闭Statement
try{
smt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
//关闭Connection
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
//System.out.println("数据库连接已关闭: "+conn);
}
}
(3)DBConn1_Demo1.java:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConn1_Demo1{
public static void main(String[] args){
/* 1 执行查询操作 */
DBConn1 db1 = new DBConn1();
String sql1 = "select * from emp";//字符串型的sql语句末尾不要带分号
//执行SQL语句,返回结果集
ResultSet rs1 = db1.executeQuery(sql1);
//操作结果集
try{
while(rs1.next()){
System.out.println(rs1.getInt(1) + "\t"
+ rs1.getString(2)+"\t"
+ rs1.getString(3));
}
}
catch(SQLException e){
e.printStackTrace();
}
db1.close();
/* 2 执行DDL操作 */
DBConn1 db2 = new DBConn1();
String sql2 = "create table demo(id int, name varchar(10), age int)";
int result1 = db2.executeUpdate(sql2);
System.out.println("有" + result1 + "条记录受到影响!");
db2.close();
/* 3 执行插入操作 */
DBConn1 db3 = new DBConn1();
String sql3 = "insert into demo values(1, 'huhu', 18)";
int result2 = db3.executeUpdate(sql3);
System.out.println("有" + result2 + "条记录受到影响!");
db3.close();
/* 4 执行查询操作 */
DBConn1 db4 = new DBConn1();
String sql4 = "select * from demo";//字符串型的sql语句末尾不要带分号
//执行SQL语句,返回结果集
ResultSet rs2 = db4.executeQuery(sql4);
//操作结果集
try{
while(rs2.next()){
System.out.println(rs2.getInt(1) + "\t"
+ rs2.getString(2)+"\t"
+ rs2.getString(3));
}
}
catch(SQLException e){
e.printStackTrace();
}
db4.close();
/* 5 执行删除操作 */
DBConn1 db5 = new DBConn1();
String sql5 = "delete from demo";
int result3 = db5.executeUpdate(sql5);
System.out.println("有" + result3 + "条记录受到影响!");
db5.close();
/* 6 执行查询操作 */
DBConn1 db6 = new DBConn1();
String sql6 = "select * from demo";//字符串型的sql语句末尾不要带分号
//执行SQL语句,返回结果集
ResultSet rs3 = db6.executeQuery(sql6);
//操作结果集
try{
while(rs3.next()){
System.out.println(rs3.getInt(1) + "\t"
+ rs3.getString(2)+"\t"
+ rs3.getString(3));
}
}
catch(SQLException e){
e.printStackTrace();
}
db6.close();
/* 7 执行DDL操作 */
DBConn1 db7 = new DBConn1();
String sql7 = "drop table demo";
int result = db7.executeUpdate(sql7);
System.out.println("有" + result + "条记录受到影响!");
db7.close();
}
}
运行结果: