现在情况如下,我采用两种方式连接数据库,一种是将连接的参数保存在配置文件当中,另外一种是直接在java代码中连接数据库,进行对数据库的操作,但是问题是现在采用配置文件的连接方式出现了问题,并且报错提示我的是连接参数有问题,不知道该怎么解决?谢谢各位帮忙!!!import java.sql.*;public class DatabaseTest
{
Connection con;
Statement sta;
ResultSet rs;
String driver;
String url;
String user;
String pwd;
public DatabaseTest()
{
driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
url="jdbc:sqlserver://localhost:1433;databaseName=WeiboByJsp";
user="sa";
pwd="sunshaoqiu";
init();
}
public void init()
{
try
{
Class.forName(driver);
System.out.println("driver is ok");
con=DriverManager.getConnection(url,user,pwd);
System.out.println("conection is ok");
sta=con.createStatement();
rs=sta.executeQuery("select * from UserNumber");
while(rs.next())
System.out.println(rs.getString("UserNo"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
new DatabaseTest();
}
}
这个是采用配置文件的方式连接数据库,但是会提示报错,不知是哪里出现了错误
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/weibo"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
maxPoolSize="20"
minPoolSize="6"
maxStatementsPerConnection="5"
acquireIncrement="2"
autoCommitOnClose="false"
maxIdleTime="3000"
idleConnectionTestPeriod="3000"
driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
user="sa"
password="sunshaoqiu"
jdbcUrl="jdbc:sqlserver://localhost:1433;DatabaseName=WeiboByJsp"
/>
</Context>
此处是使用配置文件连接数据库的java文件代码
package com.ourchr.weibo.dao.impl;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//import java.util.Random;import com.ourchr.weibo.dao.UserDAO;
import com.ourchr.weibo.entity.User;
import com.ourchr.weibo.entity.WeiBo;
import com.ourchr.weibo.util.ConnectionProvider;public class UserDAOImpl implements UserDAO{ @Override
/**
* 注册用户
*/
public void addUser(User user) {
Connection con = null;
PreparedStatement ps = null;
//Random rd1 = new Random();
String sql = "insert into user (Uid,Ualais,Uimage,Ulogon,Upasswd,Usex,Uname,Uskin,Uqq,Uschool,Uwork,Utel,Uinfo,Udatatime,Ublog)" +
"values((Select UserNo from UserNumber where No = 1),?,null,?,?,null,null,null,null,null,null,null,null,?,null)";
//String sql = "insert into user_info (code,name,password,register_time)" +
//"values(?,?,?,?)";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql); int i=0;
ps.setString(++i,user.getName());
ps.setString(++i,user.getCode());
ps.setString(++i,user.getPassword());
ps.setTimestamp(++i,new Timestamp(user.getRegisterTime().getTime()));
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* 验证用户是否存在
*/
@Override
public boolean findUser(String str) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where code=? ";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1,str);
rs = ps.executeQuery();
boolean flag = rs.next();
return flag;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return false;
}
/**
* 登录
*/
@Override
public User login(String code, String pass) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where code=? and password=?";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1,code);
ps.setString(2,pass);
rs = ps.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setCode(rs.getString("code"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setPic_100(rs.getString("pic_100"));
user.setPic_50(rs.getString("pic_50"));
user.setSex(rs.getInt("sex"));
user.setRegisterTime(rs.getTimestamp("register_time"));
return user;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
public User findUser_info(int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where id=? ";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setInt(1,id);
rs = ps.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setCode(rs.getString("code"));
user.setName(rs.getString("name"));
user.setPic_100(rs.getString("pic_100"));
user.setPic_50(rs.getString("pic_50"));
return user;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
} @Override
public List<WeiBo> searchUser(String str, User loginUser) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where code like ? and id != ?";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
String string = "%"+str+"%";
ps.setString(1, string);
ps.setInt(2, loginUser.getId());
rs = ps.executeQuery();
List<WeiBo> weibos = new ArrayList<WeiBo>();
List<User> users = new ArrayList<User>();
ListenDAOImpl lsiDaoImpl = new ListenDAOImpl();
List<Integer> isListens = lsiDaoImpl.isListen(loginUser.getId());
while(rs.next()){
User searchuser = new User();
searchuser.setId(rs.getInt("id"));
searchuser.setCode(rs.getString("code"));
searchuser.setName(rs.getString("name"));
searchuser.setPic_100(rs.getString("pic_100"));
searchuser.setPic_50(rs.getString("pic_50"));
users.add(searchuser);
WeiBo weibo = new WeiBo();
WeiBoDAOImpl weiBoDAOImpl = new WeiBoDAOImpl();
weibo = weiBoDAOImpl.searchWeiBo(searchuser.getId());
if(weibo!=null){
weibo.setSender(searchuser);
weibos.add(weibo);
}
}
for(Integer num:isListens){
for(User user:users){
if(num==user.getId()){
user.setIsListen(true);
}
}
}
return weibos;
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
} @Override
public int findCountUser(String str, User loginUser) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select count(*) from user_info where code like ? and id != ?";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, str);
ps.setInt(2, loginUser.getId());
rs = ps.executeQuery();
if(rs.next()){
int num = rs.getInt(1);
return num;
}
} catch (Exception e) {
throw new RuntimeException();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
throw new RuntimeException();
}
}
return -1;
}
}
这里是出错部分提示
2013-4-7 16:43:55 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@ab7165 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
2013-4-7 16:43:55 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run2013-4-7 16:44:09 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1b5a5cf -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败。
数据库sql
{
Connection con;
Statement sta;
ResultSet rs;
String driver;
String url;
String user;
String pwd;
public DatabaseTest()
{
driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
url="jdbc:sqlserver://localhost:1433;databaseName=WeiboByJsp";
user="sa";
pwd="sunshaoqiu";
init();
}
public void init()
{
try
{
Class.forName(driver);
System.out.println("driver is ok");
con=DriverManager.getConnection(url,user,pwd);
System.out.println("conection is ok");
sta=con.createStatement();
rs=sta.executeQuery("select * from UserNumber");
while(rs.next())
System.out.println(rs.getString("UserNo"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
new DatabaseTest();
}
}
这个是采用配置文件的方式连接数据库,但是会提示报错,不知是哪里出现了错误
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/weibo"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
maxPoolSize="20"
minPoolSize="6"
maxStatementsPerConnection="5"
acquireIncrement="2"
autoCommitOnClose="false"
maxIdleTime="3000"
idleConnectionTestPeriod="3000"
driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
user="sa"
password="sunshaoqiu"
jdbcUrl="jdbc:sqlserver://localhost:1433;DatabaseName=WeiboByJsp"
/>
</Context>
此处是使用配置文件连接数据库的java文件代码
package com.ourchr.weibo.dao.impl;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//import java.util.Random;import com.ourchr.weibo.dao.UserDAO;
import com.ourchr.weibo.entity.User;
import com.ourchr.weibo.entity.WeiBo;
import com.ourchr.weibo.util.ConnectionProvider;public class UserDAOImpl implements UserDAO{ @Override
/**
* 注册用户
*/
public void addUser(User user) {
Connection con = null;
PreparedStatement ps = null;
//Random rd1 = new Random();
String sql = "insert into user (Uid,Ualais,Uimage,Ulogon,Upasswd,Usex,Uname,Uskin,Uqq,Uschool,Uwork,Utel,Uinfo,Udatatime,Ublog)" +
"values((Select UserNo from UserNumber where No = 1),?,null,?,?,null,null,null,null,null,null,null,null,?,null)";
//String sql = "insert into user_info (code,name,password,register_time)" +
//"values(?,?,?,?)";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql); int i=0;
ps.setString(++i,user.getName());
ps.setString(++i,user.getCode());
ps.setString(++i,user.getPassword());
ps.setTimestamp(++i,new Timestamp(user.getRegisterTime().getTime()));
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* 验证用户是否存在
*/
@Override
public boolean findUser(String str) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where code=? ";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1,str);
rs = ps.executeQuery();
boolean flag = rs.next();
return flag;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return false;
}
/**
* 登录
*/
@Override
public User login(String code, String pass) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where code=? and password=?";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1,code);
ps.setString(2,pass);
rs = ps.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setCode(rs.getString("code"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setPic_100(rs.getString("pic_100"));
user.setPic_50(rs.getString("pic_50"));
user.setSex(rs.getInt("sex"));
user.setRegisterTime(rs.getTimestamp("register_time"));
return user;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
public User findUser_info(int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where id=? ";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setInt(1,id);
rs = ps.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setCode(rs.getString("code"));
user.setName(rs.getString("name"));
user.setPic_100(rs.getString("pic_100"));
user.setPic_50(rs.getString("pic_50"));
return user;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
} @Override
public List<WeiBo> searchUser(String str, User loginUser) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where code like ? and id != ?";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
String string = "%"+str+"%";
ps.setString(1, string);
ps.setInt(2, loginUser.getId());
rs = ps.executeQuery();
List<WeiBo> weibos = new ArrayList<WeiBo>();
List<User> users = new ArrayList<User>();
ListenDAOImpl lsiDaoImpl = new ListenDAOImpl();
List<Integer> isListens = lsiDaoImpl.isListen(loginUser.getId());
while(rs.next()){
User searchuser = new User();
searchuser.setId(rs.getInt("id"));
searchuser.setCode(rs.getString("code"));
searchuser.setName(rs.getString("name"));
searchuser.setPic_100(rs.getString("pic_100"));
searchuser.setPic_50(rs.getString("pic_50"));
users.add(searchuser);
WeiBo weibo = new WeiBo();
WeiBoDAOImpl weiBoDAOImpl = new WeiBoDAOImpl();
weibo = weiBoDAOImpl.searchWeiBo(searchuser.getId());
if(weibo!=null){
weibo.setSender(searchuser);
weibos.add(weibo);
}
}
for(Integer num:isListens){
for(User user:users){
if(num==user.getId()){
user.setIsListen(true);
}
}
}
return weibos;
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
} @Override
public int findCountUser(String str, User loginUser) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select count(*) from user_info where code like ? and id != ?";
try {
con = ConnectionProvider.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, str);
ps.setInt(2, loginUser.getId());
rs = ps.executeQuery();
if(rs.next()){
int num = rs.getInt(1);
return num;
}
} catch (Exception e) {
throw new RuntimeException();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (Exception e2) {
throw new RuntimeException();
}
}
return -1;
}
}
这里是出错部分提示
2013-4-7 16:43:55 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@ab7165 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
2013-4-7 16:43:55 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run2013-4-7 16:44:09 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1b5a5cf -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败。
数据库sql
package com.ourchr.weibo.util;import java.sql.Connection;
import javax.sql.DataSource;/**
* 连接池技术
* @author 201107019
*
*/public class ConnectionProvider {
private static DataSource ds = null;
public static void setDataSource(DataSource ds){
ConnectionProvider.ds = ds;
}
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
谢谢您!!非常感谢!我不知道哪儿出错了,还望指教……