概述
Oracle 数据库连接实例
package com.sungoal.etl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCTest {
// 创建一个数据库连接
public static Connection getConnection() {
Connection connection = null;
String USERNAMR = "sungoal2";
String PASSWORD = "sungoal2";
String DRVIER = "oracle.jdbc.OracleDriver";
String URL = "jdbc:oracle:thin:@192.168.12.203:1521:orcl";
try {
Class.forName(DRVIER);
connection = DriverManager.getConnection(URL, USERNAMR, PASSWORD);
System.out.println("成功连接数据库");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return connection;
}
// 关闭资源
public static void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement) {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection connection = JDBCTest.getConnection();
// 准备sql语句
String sql = "selec * from student";
// 创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
System.out.println(sql);
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
preparedStatement.setFetchSize(1000); // 每次获取1万条记录
// preparedStatement.setFetchDirection(ResultSet.FETCH_REVERSE);
resultSet = preparedStatement.executeQuery();
System.out.println("ps.getQueryTimeout():" + preparedStatement.getQueryTimeout());
System.out.println("ps.getFetchSize():" + preparedStatement.getFetchSize());
System.out.println("ps.getFetchDirection():" + preparedStatement.getFetchDirection());
System.out.println("ps.getMaxFieldSize():" + preparedStatement.getMaxFieldSize());
System.out.println("查询成功---" + resultSet.toString());
int col = resultSet.getMetaData().getColumnCount();
// System.out.println(col);
int m = 0;
while (resultSet.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(resultSet.getString(i));
System.out.print("tt");
}
m++;
System.out.println("数据--" + m);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTest.close(resultSet);
JDBCTest.close(preparedStatement);
JDBCTest.close(connection);
}
}
}
最后
以上就是妩媚月光为你收集整理的JDBC数据库连接实例的全部内容,希望文章能够帮你解决JDBC数据库连接实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复