首先在myeclipse下建立一个项目(Test)
Entity包:
对象属性,添加get,set方法,不赘述。
Util包:
public class DBConection {
private static Connection conn;
public static Connection getConnection(){
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("database");
conn = ds.getConnection();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
Dao包:
public class UserDao {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private User user = null;
private ArrayList<User> userlist = null;
public ArrayList<User> getAll(){
conn = DBConection.getConnection();
userlist = new ArrayList<User>();
try {
ps = conn.prepareStatement("select * from userinfo");
rs = ps.executeQuery();
while(rs.next()){
user = new User();
user.setName(rs.getString(1));
user.setSex(rs.getString(2));
user.setAge(rs.getShort(3));
userlist.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userlist;
}
简单jsp页面:
<body>
<%
User user = new User();
UserDao ud = new UserDao();
user = ud.getAll().get(1);
%>
<%=user.getName() %>
<%=user.getAge() %>
<%=user.getSex() %>
</body>
在weblogic 控制台配置数据源:
输入地址完成: