import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
public class dbTest {
/**
* @param args
*/
public static void main(String[] args) {
Connection connection = null ;
Map<String,Object> map = new HashMap<String,Object>();
try {
String table = "testtable";
String url = "127.0.0.1";
String name = "name";
String password = "password";
String port = "1521";
String parentId = "orcl";
String URL = "jdbc:oracle:thin:@" + url + ":" + port + ":" + parentId ;
String driverName = "oracle.jdbc.OracleDriver"; //加载驱动
Class.forName(driverName).newInstance();
Statement stmt= null;
ResultSet result = null;
PreparedStatement pstmt = null;
connection = DriverManager.getConnection(URL , name , password);
stmt = connection.createStatement();
pstmt = connection.prepareStatement("SELECT * FROM "+table);
result = pstmt.executeQuery();
while (result.next()) {//ID 和 NAME 是 查询table表中的字段
System.out.print(result.getInt("ID"));
System.out.print(result.getString("NAME"));
}
result.close();
stmt.close();
connection.close();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}
oracle跨库访问查询、数据获取、打印(测试【已通】)
最新推荐文章于 2024-08-16 10:08:41 发布