Java初学者笔记:JDBC连接Oracle数据库
2009-11-18 21:02:44 来源:WEB开发网核心提示:view plaincopy to clipboardPRint?/** * JDBCTest.java * 编译脚本cd\cd D:\JavaHome\tempd:javac JDBCTest.javajavaJDBCTestpause*/import java.sql.*; // WindowsXP使用JDBC连接
view plaincopy to clipboardPRint?
/**
* JDBCTest.java
* 编译脚本
cd\
cd D:\JavaHome\temp
d:
javac JDBCTest.java
java JDBCTest
pause
*/
import java.sql.*;
// WindowsXP使用JDBC连接Oracle10g数据库
// Oracle10g 客户端安装在: "D:\OraClient10g"
public class JDBCTest {
public static void main(String args[ ]) {
Connection connection = null;
Statement statement = null;
try {
// Load the JDBC Driver
// 必须在系统环境变量 CLASSPATH 添加 "D:\OraClient10g\jdbc\lib\classes12.zip"
String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
// 找到 "D:\OraClient10g\NETWORK\ADMIN\tnsnames.ora" 文件, 将 (DESCRIPTION=...)拷贝到下面
String URL = "jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = mine)(PORT = 1521)) )(CONNECT_DATA =(SID = cheung)(SERVER = DEDICATED)))";
Class.forName(DBDRIVER).newInstance();
// Connect to the database
connection = DriverManager.getConnection(URL, "scott", "tiger");
// Obtain a statement object
statement = connection.createStatement();
// Execute the SQL
String sql = "select * from JDBC_TEST";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
{
System.out.println(rs.getString(1)); // 列的索引: 1-based
}
rs.close();
}
// Don't try this at home, catch SQLException and all others
catch( Exception e ) {
e.printStackTrace();
}
finally {
// Time to close everthing up.
if( statement != null ) {
try {
statement.close();
}
catch( SQLException e ){
} // nothing we can do
}
if( connection != null ) {
try {
connection.close();
}
catch( SQLException e ){
} // nothing we can do
}
}
}
}
/**
* JDBCTest.java
* 编译脚本
cd\
cd D:\JavaHome\temp
d:
javac JDBCTest.java
java JDBCTest
pause
*/
import java.sql.*;
// WindowsXP使用JDBC连接Oracle10g数据库
// Oracle10g 客户端安装在: "D:\OraClient10g"
public class JDBCTest {
public static void main(String args[ ]) {
Connection connection = null;
Statement statement = null;
try {
// Load the JDBC Driver
// 必须在系统环境变量 CLASSPATH 添加 "D:\OraClient10g\jdbc\lib\classes12.zip"
String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
// 找到 "D:\OraClient10g\NETWORK\ADMIN\tnsnames.ora" 文件, 将 (DESCRIPTION=...)拷贝到下面
String URL = "jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = mine)(PORT = 1521)) )(CONNECT_DATA =(SID = cheung)(SERVER = DEDICATED)))";
Class.forName(DBDRIVER).newInstance();
// Connect to the database
connection = DriverManager.getConnection(URL, "scott", "tiger");
// Obtain a statement object
statement = connection.createStatement();
// Execute the SQL
String sql = "select * from JDBC_TEST";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
{
System.out.println(rs.getString(1)); // 列的索引: 1-based
}
rs.close();
}
// Don't try this at home, catch SQLException and all others
catch( Exception e ) {
e.printStackTrace();
}
finally {
// Time to close everthing up.
if( statement != null ) {
try {
statement.close();
}
catch( SQLException e ){
} // nothing we can do
}
if( connection != null ) {
try {
connection.close();
}
catch( SQLException e ){
} // nothing we can do
}
}
}
}
[]
- ››JavaScript拖拽原理的实现
- ››javascript事件列表解说
- ››Javascript代码优化工具UglifyJS
- ››Java Bean属性值动态设置
- ››JavaScript Confirm 失效的解决办法
- ››JavaScript页面内拖拽原理分析
- ››javascript中select的常用操作
- ››javascript+css无刷新实现页面样式的更换
- ››Java Web Services:不使用客户端证书的WS-Securit...
- ››Java开发2.0: 使用Amazon SimpleDB实现云存储,第...
- ››Java异常处理及其应用
- ››Java中遍历大容量map的正确方法
更多精彩
赞助商链接