关于struts中数据库的使用

来源:互联网 发布:迷宫的十字路口 知乎 编辑:程序博客网 时间:2024/06/06 01:09

 在以往的jsp+javabean的套路中,连接数据库的方式通常写在bean.java里的构造方法里,貌似如下情况(javabean):

以Oracle_bean

public Orcale_bean()
 { 
  //数据库连接
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   try {
    conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:CHNIX","scott","tiger");
    stat=conn.createStatement();
    System.out.println("success");
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

但是在struts中,数据库的连接在struts-config.xml中设置,具体套路如下:

在data-source右键:new datasouce

填写表单内容如下:

Description:空
Driver class:oracle.jdbc.driver.OracleDriver
Url:jdbc:oracle:thin:@127.0.0.1:1521:chnix
User:scott
Password:tiger
Login time out:空
Min count:3
Max count:10


之后就可以在Action或其他地方就可以直接通过数据库连接池使用数据库及其相关了。

例如下:

ListAction中:
conn = this.getDataSource(request).getConnection();//从数据源.连接池中得到信息赋给conn


2006年8月28日 李涛