我在struts-config.xml中配置了数据源:<data-source type="oracle.jdbc.pool.OracleDataSource">
<set-property property="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<set-property property="url"
value="jdbc:oracle:thin:@192.168.0.204:1521:yiyao" />
<set-property property="username"
value="cra" />
<set-property property="password"
value="medlink" />
<set-property property="maxActive"
value="10" />
<set-property property="maxWait"
value="5000" />
<set-property property="defaultAutoCommit"
value="false" />
<set-property property="defaultReadOnly"
value="false" />
</data-source> 然后在登录的action中写下面的代码:
DataSource ds=null;
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try{
try{
System.out.println("进入try");
ds=getDataSource(request);
conn=ds.getConnection();
if (conn!=null){
System.out.println("取得连接");}
else {System.out.println("连接为空");}
strsql="select * from centerUser where username=? and password=?";
pstmt=conn.prepareStatement(strsql);
pstmt.setString(1, username);
pstmt.setString(2, password);
System.out.println("sql语句完成");
rs=pstmt.executeQuery();
System.out.println("查询执行完成");
rs.first();
if (rs.next()) {
System.out.println(rs.getString("truename"));
}else{
System.out.println("fail");
}
}catch (Exception e) {
if (rs==null){
System.out.println("rs为空");}
else{
System.out.println("rs不为空");
}
if (conn==null){
System.out.println("连接为空");}
else{
System.out.println("连接不为空");
}
if (ds==null){
System.out.println("数据源为空");}
else{
System.out.println("数据源不为空");
}
}
}finally{
try{
if (rs!=null){
rs.close();}
if (pstmt!=null){
pstmt.close();}
if (conn!=null){
conn.close();}
}catch (Exception e){
}
} 得到的结果是:
进入try
rs为空
连接为空
数据源不为空
<set-property property="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<set-property property="url"
value="jdbc:oracle:thin:@192.168.0.204:1521:yiyao" />
<set-property property="username"
value="cra" />
<set-property property="password"
value="medlink" />
<set-property property="maxActive"
value="10" />
<set-property property="maxWait"
value="5000" />
<set-property property="defaultAutoCommit"
value="false" />
<set-property property="defaultReadOnly"
value="false" />
</data-source> 然后在登录的action中写下面的代码:
DataSource ds=null;
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try{
try{
System.out.println("进入try");
ds=getDataSource(request);
conn=ds.getConnection();
if (conn!=null){
System.out.println("取得连接");}
else {System.out.println("连接为空");}
strsql="select * from centerUser where username=? and password=?";
pstmt=conn.prepareStatement(strsql);
pstmt.setString(1, username);
pstmt.setString(2, password);
System.out.println("sql语句完成");
rs=pstmt.executeQuery();
System.out.println("查询执行完成");
rs.first();
if (rs.next()) {
System.out.println(rs.getString("truename"));
}else{
System.out.println("fail");
}
}catch (Exception e) {
if (rs==null){
System.out.println("rs为空");}
else{
System.out.println("rs不为空");
}
if (conn==null){
System.out.println("连接为空");}
else{
System.out.println("连接不为空");
}
if (ds==null){
System.out.println("数据源为空");}
else{
System.out.println("数据源不为空");
}
}
}finally{
try{
if (rs!=null){
rs.close();}
if (pstmt!=null){
pstmt.close();}
if (conn!=null){
conn.close();}
}catch (Exception e){
}
} 得到的结果是:
进入try
rs为空
连接为空
数据源不为空
conn=ds.getConnection(); 有问题;
给你段实例,希望能有所帮助。public static Connection getConnection(){ Connection c = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
c = DriverManager.getConnection("jdbc:oracle:thin:@32.36.3.180:1521:ora9i","photo","photo");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("myconn():"+e.getMessage());
}
catch(java.sql.SQLException e)
{
System.out.println("conn():"+e.getMessage());
}
catch(Exception e)
{
System.out.println("Driver error!");
System.err.println(e.getMessage());
}
return c;
} }
谢谢你的提醒,System.out.println("conn():"+e.getMessage());把错误显示出来,
修改后
Class.forName("org.apache.commons.dbcp.BasicDataSource");
conn = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.204)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=yiyao)(SERVER=DEDICATED)))","cra","medlink");
就可以正常连接了.可是用这个方法,我配置的struts里的数据源不就没用了......
有没有通过struts配置的那个数据来连接的方法?
一般将数据库连接池在web容器中配置,然后通过Context接口来得到这个jndi数据源
ds=getDataSource(request);这个方法不是我写的,api里面的吧。我抄来的。
因为执行到那步,ds不为空,所以也没想到这个会有问题。
我现在连接数据库还是独立的,没有用到jndi