如果你操作的MySQL数据库,请参见:http://blog.csdn.net/wolfofsiberian/article/details/39852569
Oracle数据库的Java操作,其基本步骤与其他的数据库一样的。
1、下载驱动程序
请到官网下载Oracle 12c的jdbc驱动包,使用的时候请根据自己的JDK版本来区别使用jdbc的版本。
Oracle 12c的jdbc驱动下载页面为:
http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html
JDK7请使用ojdbc7.jar JDK6请使用ojdbc6.jar
2、将驱动包配置到classpath,否则会出现编译错误
环境变量的设置相信大家已经非常熟悉,但是请记得,在添加oracle jdbc包路径的时候,请在最后添加一个分号结束。具体为什么请参见:http://blog.csdn.net/wolfofsiberian/article/details/39926099
3、Java代码实现
/**
* Java操作Oracle数据库
* @author:WolfOfSiberian
* @email:QuietWolf@yeah.net
* @create time:2014/10/09 10:13
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class OperateOracle{
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
/**
* To Get Connection Object
*/
public Connection getConnection()throws Exception{
//1. Load Driver
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//2. get Connection
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","scott","tiger");
System.out.println("Congratulation to you! Connected Oracle successfully!");
return conn;
}
/**
* Executes the given SQL statement, which returns a single ResultSet object.
* @param sql an SQL statement to be sent to the database, typically a static SQL SELECT statementSQL String
* @return a ResultSet object that contains the data produced by the given query; never null
* @Throws
*/
public ResultSet executeQuery(String sql)throws Exception{
getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs;
}
/**
* free the Resource
*/
public void freeResultSet()throws SQLException{
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}
/**
* output format
*/
public static String formatOutput(final String str,final int maxLen){
int strLen = str.length();
String returnStr=str;
for(int i = (maxLen - strLen);i > 0;i--){
returnStr = returnStr + " ";
}
return returnStr;
}
//main 入口
public static void main(String[] args)throws Exception{
OperateOracle oracle=new OperateOracle();
//ResultSet tempRs = oracle.executeQuery("SELECT * FROM dept;");//sql语句后此处,不要加分号,否则会运行报错。
ResultSet tempRs = oracle.executeQuery("SELECT * FROM dept");
int deptno = 0;
String dname = null;
String loc = null;
int rowCount = 0;
//output format
String splitStr = " ";
System.out.println(formatOutput("DEPTNO",2)+splitStr+formatOutput("DNAME",14)+splitStr+formatOutput("LOC",13));
System.out.println("----------------------------------------");
while(tempRs.next()){
rowCount++;
deptno = tempRs.getInt("DEPTNO");
dname = tempRs.getString("DNAME");
loc = tempRs.getString("LOC");
System.out.println(formatOutput(String.valueOf(deptno),2)+splitStr+formatOutput(dname,14)+splitStr+formatOutput(loc,13));
}
//free
oracle.freeResultSet();
}
}
运行结果:
F:\01 Java\01 Project\00 ConnectDatabase>java OperateOracle
Congratulation to you! Connected Oracle successfully!
DEPTNO DNAME LOC
----------------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON