关闭

struts连接数据库

标签: struts数据库importnullswtjdbc
1547人阅读 评论(0) 收藏 举报
分类:
struts连接数据库

struts-config.xml添加

在<struts-config>里面加上
   
    <!-- ========== Data Source Configuration =============================== -->
    <data-sources>
        <data-source key="org.apache.struts.action.DATA_SOURCE" type="org.apache.struts.util.GenericDataSource">
            <set-property property="password" value="shiwtshiwt" />
            <set-property property="minCount" value="" />
            <set-property property="maxCount" value="" />
            <set-property property="user" value="swt" />
            <set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
            <set-property property="description" value="" />
            <set-property property="url" value="jdbc:mysql://localhost:3306/smsgroup?autoReconnect=true" />
            <set-property property="readOnly" value="false" />
            <set-property property="autoCommit" value="false" />
            <set-property property="loginTimeout" value="" />
        </data-source>
    </data-sources>

在你的Action类的excute即可执行以下连接数据库代码

import javax.servlet.ServletContext;
import javax.sql.*;   

Connection conn = null;
   Statement stmt = null;
   ResultSet rs = null;
   String tel = "tel";
   ServletContext context = this.servlet.getServletContext();
   DataSource dataSource = (DataSource)context.getAttribute("org.apache.struts.action.DATA_SOURCE");
  
   try {
    conn = dataSource.getConnection();
    stmt = conn.createStatement();
    //rs = stmt.executeQuery("select * from user where age = 24");      
    rs = stmt.executeQuery("select * from c_13760827783 where id = 6");
    if( rs.next() ){
     tel = rs.getString("name");
     System.out.print(tel);
    }
   }catch (SQLException e) {
    System.err.println(e.getMessage());
   }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72756次
    • 积分:1083
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:39篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论
    MyEclipse Struts 快速上手