2
我的代碼連接到數據庫使用BasicDataSource如下,參考Ans我想我的代碼創建一個物理連接數據庫ONCE,然後提供池連接後。 我的問題是,我的代碼是否提供池連接?,如果是這樣,我應該保持代碼的連接部分在以下方法或在構造函數中提供高效的池?以及班級狀態的註釋應該如何? @Stateful?如何高效地實現連接池?
// Connection section start
BasicDataSource bs = new BasicDataSource();
PreparedStatement ps = null;
Connection con = null;
try {
bs.setDriverClassName("com.mysql.jdbc.Driver");
bs.setUrl("jdbc:mysql://localhost/myDb");
bs.setUsername("root");
bs.setPassword("");
con = bs.getConnection();
System.out.println("Connecting");
// Connection section end
ps = con.prepareStatement("Select *"
+ " from Client WHERE Username = ? ");
ps.setString(1, username);
ResultSet r = ps.executeQuery();
if (r.next()) {
con.close();
return "true";
}
使用Apache Commonds DBCP連接池。不要自己寫這些東西。 – EJP 2013-02-12 01:12:38
@EJP,哪些東西?你會舉個例子嗎? – 2013-02-12 01:29:53