oracle跨库访问查询、数据获取、打印(测试【已通】)


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 map = new HashMap();
		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跨库访问查询、数据获取、打印(测试【已通】))