在web.xml中配置数据库连接池:
1、首先,在web.xml中配置数据库连接池:
2、在context.xml中配置数据库连接:
maxActive="100" maxIdle="30" maxWait="10000" username="username" password="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test"/> 3、在JavaBean中编写查询数据库的代码: public class UserDao { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public List List Connection conn = null; try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT * FROM USER"); ResultSet rs = ps.executeQuery(); while (rs.next()) { User user = new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setAge(rs.getInt("age")); users.add(user); } rs.close(); ps.close(); return users; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } } } 本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(31条)