在Java编程中,连接Oracle数据库是一项基础且重要的任务,使我们能够与Oracle数据库进行交互。以下是简单的Java连接Oracle的测试代码示例。首先,引入Oracle的JDBC驱动:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
在GetMetaData.java
中:
public class GetMetaData {
public static void main(String[] args) {
String url = \"jdbc:oracle:thin:@//hostname:port/service_name\";
String username = \"your_username\";
String password = \"your_password\";
try {
Class.forName(\"oracle.jdbc.driver.OracleDriver\");
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.getMetaData();
while (rs.next()) {
System.out.println(rs.getString(...));
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}