jsp容器:resin3.2.0(pro);数据库mysql5.0
resin.conf中连接池配置:
<database>
<jndi-name>jdbc/jjj</jndi-name>
<driver type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/lxtjng</url>
<user>kk</user>
<password>kk</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>1000</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
WEB-INF下的web.xml中配置如下:
<database>
<jndi-name>jdbc/jjj</jndi-name>
<driver type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/jjj?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull</url>
<user>kk</user>
<password>kk</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>1000</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
以下是调用数据库jjj的javebean:
package db; import java.util.Properties;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
public class lxtconndb {
Connection connjng = null; //数据库连接
ResultSet rs = null; //记录集
String jndi="java:comp/env/jdbc/jjj";//jndi
Statement stmt=null;
public lxtconndb(){
try {Properties p = new Properties();
Context env = new InitialContext(p);
DataSource pool = (DataSource) env.lookup(jndi);
if (pool == null)
throw new Exception("jdbc/mysql is an unknown DataSource");
connjng = pool.getConnection();
stmt = connjng.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch (Exception e) {
System.out.println("naming:" + e.getMessage());
} }
public ResultSet executeQuery(String sql){
try {
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("数据库查询失败db.executeQuery: " + ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql){
boolean bupdate=false;
rs = null;
try {
int rowCount = stmt.executeUpdate(sql);//如果不成功,bupdate就会返回0
if(rowCount!=0)
{
bupdate=true;}
}
catch(SQLException ex) {
System.err.println("数据库更新db.executeUpdate失败: " + ex.getMessage());
}
return bupdate;
}
/**
* 关闭连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (connjng != null) {
connjng.close();
connjng = null;
}
} catch (SQLException se) {
System.out.println("close error: " + se.getMessage());
}
}
}
在网页中使用<jsp:useBean id="joyrisingbean" scope="page" class="db.lxtconndb"/>来调用数据库。
问题:
1、数据库连接池是否正确,如何改善其性能(最好附改后源码--小弟初学!)
2、出现怪现象:当数据表中有值时(非空)时出现java.lang.NullPointerException错误,无值时倒可以正常运行,不知道何故?改用无连接池连接数据库后正常,无任何错误!
请赐教!
resin.conf中连接池配置:
<database>
<jndi-name>jdbc/jjj</jndi-name>
<driver type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/lxtjng</url>
<user>kk</user>
<password>kk</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>1000</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
WEB-INF下的web.xml中配置如下:
<database>
<jndi-name>jdbc/jjj</jndi-name>
<driver type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/jjj?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull</url>
<user>kk</user>
<password>kk</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>1000</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
以下是调用数据库jjj的javebean:
package db; import java.util.Properties;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
public class lxtconndb {
Connection connjng = null; //数据库连接
ResultSet rs = null; //记录集
String jndi="java:comp/env/jdbc/jjj";//jndi
Statement stmt=null;
public lxtconndb(){
try {Properties p = new Properties();
Context env = new InitialContext(p);
DataSource pool = (DataSource) env.lookup(jndi);
if (pool == null)
throw new Exception("jdbc/mysql is an unknown DataSource");
connjng = pool.getConnection();
stmt = connjng.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch (Exception e) {
System.out.println("naming:" + e.getMessage());
} }
public ResultSet executeQuery(String sql){
try {
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("数据库查询失败db.executeQuery: " + ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql){
boolean bupdate=false;
rs = null;
try {
int rowCount = stmt.executeUpdate(sql);//如果不成功,bupdate就会返回0
if(rowCount!=0)
{
bupdate=true;}
}
catch(SQLException ex) {
System.err.println("数据库更新db.executeUpdate失败: " + ex.getMessage());
}
return bupdate;
}
/**
* 关闭连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (connjng != null) {
connjng.close();
connjng = null;
}
} catch (SQLException se) {
System.out.println("close error: " + se.getMessage());
}
}
}
在网页中使用<jsp:useBean id="joyrisingbean" scope="page" class="db.lxtconndb"/>来调用数据库。
问题:
1、数据库连接池是否正确,如何改善其性能(最好附改后源码--小弟初学!)
2、出现怪现象:当数据表中有值时(非空)时出现java.lang.NullPointerException错误,无值时倒可以正常运行,不知道何故?改用无连接池连接数据库后正常,无任何错误!
请赐教!
网页中具体调用如下:
String sql="SELECT * from jnginfo where isgb='yes' order by visitcount DESC ";
ResultSet rs=joyrisingbean.executeQuery(sql);
我错在哪啊?请指教!!