jdbc oracle数据库访问

本文通过一个具体的Java JDBC示例,展示了如何使用JDBC连接Oracle数据库并执行SQL查询。文章包括了必要的驱动加载、数据库连接建立、SQL语句执行及结果处理等步骤,并强调了正确关闭资源的重要性。

package jdbcTest;

import java.sql.*;

public class TestJDBC {

	public static void main(String[] args) {
		ResultSet rs = null;
		Statement stmt = null;
		Connection conn = null;
		try {
			//实例话驱动类
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@10.20.128.180:1526:d0claim";
			String user = "claimpptopr";
			String password = "paic1234";
			//建立到数据库的连接
			conn = DriverManager.getConnection(url,user,password);
			//将数据发送到数据库中
			stmt = conn.createStatement();
			//执行语句
			rs = stmt.executeQuery("select * from claim_product_class cc where cc.product_class_code like 'S%'");
			System.out.println("查询结果如下:");
			while (rs.next()) {
				System.out.println(rs.getString("PRODUCT_CLASS_NAME"));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
				System.out.println("==================");
				System.out.println("数据库已关闭...");
				System.out.println("==================");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}


数据库配置串为:claimpptopr/paic1234@10.20.128.180:1526/d0claim


用上面连接串来配置url,user和password。


建立好工程后,直接运行程序,发现报错误,后来上网求教,得知没有引入ojdbc的jar包。




后运行成功,得到正确结果。



注意两点:(1)配置串

                    (2)引入jar包




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值