oracle数据库底层的query方法

public class OracDBUtils {
	static Properties p = new Properties();
	static{
		InputStream is =(OracDBUtils.class.getResourceAsStream("oraclejdbc.properties"));
		try {
			p.load(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws Exception {
		String url = p.getProperty("url");
		String driverClass = p.getProperty("driverClass");
		String username = p.getProperty("username");
		String password = p.getProperty("password");
		Class.forName(driverClass);
		Connection conn = DriverManager.getConnection(url, username, password);
//		System.out.println(conn);
		return conn;
	}
	public static List<Map> query(String sql) throws Exception{
		
		Connection conn =getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		ResultSetMetaData rsmd = rs.getMetaData();
		List list = new ArrayList();
		
		//鑾峰彇鍒楃殑涓暟
		int columCount = rsmd.getColumnCount();
		while(rs.next()){
			Map map = new HashMap();
			for(int i =1;i<=columCount;i++){
				String colName = rsmd.getColumnName(i);
				String colValue = rs.getString(i);
				map.put(colName, colValue);
			}
			list.add(map);
		}
		rs.close();
		pst.close();
		conn.close();
		return list;
	}
	public static int execute(String sql) throws Exception{
		Connection conn =getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		int i = pst.executeUpdate();
		pst.close();
		conn.close();
		return i;
	}
	


猜你喜欢

转载自blog.csdn.net/qq_37928350/article/details/79089083
今日推荐