日期:2014-05-20  浏览次数:20986 次

JDBC连接问题
public class TTTT {
public static void main(String[] args) throws Exception{
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1434; DatabaseName=DairySystem"; // 连接服务器和数据库
String userName = "sa"; // 默认用户名
String userPwd = "zhang"; // 密码

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(dbURL, userName, userPwd);
DatabaseMetaData metaData=conn.getMetaData();
System.out.println(metaData.getMaxConnections());
conn.close();//conn不关闭是,下面这句报错
DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("OK");
}
}


sqlserver 2005的并发连接不是无限制的嘛 为什么一定要关闭前面那个连接 然后才能新建连接?????

------解决方案--------------------
class Test 
{
public static void main (String[] args) 
{
try
{
Connection conn = getConnection();
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery( "SELECT * FROM AAA");
result.next();
System.out.println(result.getString(1));
result.close();
stat.close();
conn.close();
}
catch(SQLException ex)
{
while(ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

public static Connection getConnection()
throws SQLException,IOException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();
String drivers = props.getProperty( "jdbc.drivers ");
if (drivers != null)
System.setProperty("jdbc.drivers",drivers);
String url = props.getProperty( "jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url,username,password);
}
}