连接oracle数据库的java代码
时间: 2023-09-01 21:13:06 浏览: 47
以下是连接Oracle数据库的Java代码示例:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleConnection {
public static void main(String[] args) {
Connection connection = null;
try {
// Load the Oracle JDBC driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Connect to the database
String url = "jdbc:oracle:thin:@localhost:1521:XE"; // replace with your Oracle database URL
String user = "username"; // replace with your Oracle database username
String password = "password"; // replace with your Oracle database password
connection = DriverManager.getConnection(url, user, password);
// Do something with the connection
System.out.println("Connected to Oracle database.");
// Close the connection
connection.close();
System.out.println("Disconnected from Oracle database.");
} catch (ClassNotFoundException e) {
System.out.println("Oracle JDBC driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Failed to connect to Oracle database.");
e.printStackTrace();
}
}
}
```
请注意,在此示例中,我们使用Oracle JDBC驱动程序来连接数据库。您需要下载并安装此驱动程序,然后将其添加到您的Java项目中,以便能够与Oracle数据库通信。您还需要替换示例代码中的数据库URL,用户名和密码,以便与您的Oracle数据库匹配。