首先先写一个小的web程序
目录如下:
具体代码如下
package bean;
public class Student {
private String studentName;
private String sex;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student(String studentName, String sex) {
super();
this.studentName = studentName;
this.sex = sex;
}
public Student() {
super();
}
}
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import db.DBConection;
import bean.Student;
public class StudentDao {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private Student student = null;
private ArrayList<Student> studentlist = null;
public ArrayList<Student> getAll(){
conn = DBConection.getConnection();
studentlist = new ArrayList<Student>();
try {
ps = conn.prepareStatement("select * from studentinfo");
rs = ps.executeQuery();
while(rs.next()){
System.out.print("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwww");
student = new Student(rs.getString(1),rs.getString(2));
studentlist.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
}
return studentlist;
}
}
package db;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConection {
private static Connection conn;
/**
* 获取数据连接
* @return
*/
public static Connection getConnection(){
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("student");
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;
}
}
页面代码如下
<table border="1">
<tr><td>姓名</td><td>性别</td></tr>
<%
ArrayList<Student> student = new StudentDao().getAll();
System.out.println("姓名\t\t性别");
Iterator it = student.iterator();
while(it.hasNext()){
Student stu = (Student)it.next();
System.out.println(stu.getStudentName()+"\t"+stu.getSex());
out.print("<tr><td>问问"+stu.getStudentName()+"</td><td>"+stu.getSex()+"问问</td></tr>");
}
%>
</table>
自此web工程建立完毕
下面建立连接池:
将MySQL架包拷到下面位置
C:\oracle\middle\wlserver_10.3\server\lib下(这个是我的安装位置)
打开
C:\oracle\middle\user_projects\domains\base_domain\bin
下的setDomainEnv.cmd(用记事本打开)
加入如下代码:
set CLASSPATH=%PRE_CLASSPATH%;%WEBLOGIC_CLASSPATH%;%POST_CLASSPATH%;%WLP_POST_CLASSPATH%;%WL_HOME%\server\lib\mysql-connector-java-commercial-5.1.17-bin.jar
启动weblogic出现如下内容则架包放置正确:
进入weblogic的控制台点击数据源
点击新建
给数据源命名并选择数据类型
选择数据驱动
填写数据库的基本信息
完成所有之后与服务器绑定即可,基本上全部是直接点击下一步
得到的结果如下: