java 连接oracle数据库代码
时间: 2023-06-21 14:06:22 浏览: 100
这是一个简单的 Java 连接 Oracle 数据库的代码示例:
```java
import java.sql.*;
public class OracleConnection {
public static void main(String[] args) {
try {
// Load Oracle JDBC driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Connect to Oracle database
String url = "jdbc:oracle:thin:@localhost:1521:xe"; // Replace with your database URL
String user = "username"; // Replace with your username
String password = "password"; // Replace with your password
Connection con = DriverManager.getConnection(url, user, password);
// Execute SQL query
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employees"); // Replace with your table name
// Process query result
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2)); // Replace with your column names
}
// Close resources
rs.close();
stmt.close();
con.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
```
你需要先下载并安装 Oracle JDBC 驱动程序,然后将其添加到类路径中。在上面的代码中,替换 URL、用户名、密码和查询语句以适应你的数据库。
阅读全文