public List
//创建连接
Connection conn =
DBUtil.getConnection();
//创建SQL
String sql = "select * from t_emp1";
try {
//创建PreparedStatement
PreparedStatement ps =
conn.prepareStatement(sql);
//执行SQL
ResultSet rs = ps.executeQuery();
//遍历结果集
List
while(rs.next()) {
Emp emp = new Emp();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setSalary(rs.getDouble("salary"));
emp.setAge(rs.getInt("age"));
list.add(emp);
}
return list;
} catch (SQLException e) {
throw new RuntimeException(
"查询员工失败.", e);
} finally {
//关闭连接
DBUtil.close(conn);
}
}
1
2
3
CREATE [PUBLIC] DATABASE LINK link_name
CONNECT TO username IDENTIFIED BY password
USING 'tns_linkname'
public是范围, 缺省的那默认的owner就是当前用户;
tns_linkname是本地tnsname.ora中定义的链接串;
username 、password 连接的用户名和密码。
也可以使用缺省模式:
如果username和password一致
1
CREATE [PUBLIC] DATABASE LINK link_name CONNECT TO username USING 'tns_linkname'
如果远程库上存在于当前登录用户和密码一致的用户
1
CREATE [PUBLIC] DATABASE LINK link_name USING 'tns_linkname'