怎么写java程序,来实现创建一个连接去连接本地的Oracle数据库中的某一张表,谁能写一个例子啊!求教

2024-11-25 14:06:49
推荐回答(1个)
回答1:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;


public class Oracle {

public static void main(String[] args) throws SQLException,

ClassNotFoundException {

Class.forName("oracle.jdbc.driver.OracleDriver");

String url = "jdbc:oracle:thin:@127.0.0.1:1521:test";

String user = "test";

String password = "test";

Connection conn = DriverManager.getConnection(url, user, password);

PreparedStatement ps = conn.prepareStatement("select * from emp");

ResultSet rs = ps.executeQuery();

while (rs.next()) {

System.out.println(rs.getObject(1));

}

rs.close();

ps.close();

conn.close();

}

}