连接数据库出错处理办法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

没学好,又要学jsp了....

1.

安装:SQL Server 2000 Driver for JDBC Service Pack 3

下载安装JDBC SP3

/downloads/details.aspx?familyid=07287B11-0502-461 A-B138-2AA54BFDC03A&displaylang=en

里面的SetUp.exe

按照提示安装可以了.成功后有三个文件要使用:

c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar

c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar

c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar 2.测试代码

新建类文件Connect.java.

package test;

import java.*;

import java.sql.Driver;

public class Connect{

private java.sql.Connection con = null;

private final String url = "jdbc:m icrosoft:sqlserver://";

private final String serverName= "localhost";

private final String port Number = "1433";

private final String databaseName= "DBtest";

private final String userName = "sa";

private final String password = "123456";

// Informs the driver to use server a side-cursor,

// which permits m ore than one active statem ent

// on a connection.

private final String selectMethod = "cursor";

// Constructor

public Connect(){}

private String getConnectionUrl(){

return

url+serverName+":"+portNumber+";databaseName="+databaseName+";select Method="+selectMethod+";";

}

private java.sql.Connection getConnection(){

try{

Class.forNam e("com.microsoft.jdbc.sqlserver.SQLServerDriver");

con =

java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);

if(con!=null) System.out.println("Connection Successful!");

}catch(Exception e){

e.printStackTrace();

System.out.println("Error Trace in getConnection() : " +

e.getMessage());

}

return con;

}

/*

Display the driver properties, database details

*/

public void displayDbProperties(){

java.sql.DatabaseMetaData dm = null;

java.sql.ResultSet rs = null;

try{

con= this.getConnection();

if(con!=null){

dm = con.getMetaData();

System.out.println("Driver Information");

System.out.println("\t Driver Name: "+ dm.get DriverName());

System.out.println("\t Driver Version: "+ dm.get DriverVersion ());

System.out.println("\nDatabase Information ");

System.out.println("\t Database Name: "+

dm.get DatabaseProductNam e());

System.out.println("\t Database Version: "+

dm.get DatabaseProductVersion());

System.out.println("Avalilable Catalogs ");

rs = dm.getCatalogs();

while(rs.next()){

System.out.println("\tcatalog: "+ rs.getString(1));

}

rs.close();

rs = null;

closeConnection();

}else System.out.println("Error: No active Connection");

}catch(Exception e){

e.printStackTrace();

}

dm=null;

}

private void closeConnection(){

try{

if(con!=null)

con.close();

con=null;

}catch(Exception e){

e.printStackTrace();

}

}

public static void m ain(String[] args) throws Exception

{

Connect m yDbTest = new Connect();

m yDbTest.displayDbProperties();

}

}

代码来源:

/default.aspx?scid=kb;zh-cn;313100 ------------------------------------------

成功后控制台输出:

相关文档
最新文档