JDBC数据库连接实例

7 篇文章 0 订阅
2 篇文章 0 订阅

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("\t\t");
				}
				m++;
				System.out.println("数据--" + m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTest.close(resultSet);
			JDBCTest.close(preparedStatement);
			JDBCTest.close(connection);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值