微软官方测试jdbc,经修改

package com.hfuu.xl.db;

import java.*;

import org.apache.log4j.Logger;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:sqlserver://";
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "master";
private final String userName = "sa";
private final String password = "123456";
private final String selectMethod = "cursor";
private static Logger logger=Logger.getLogger(JDBCTest.class);

public Connect(){}

private String getConnectionUrl(){
// String url = "jdbc:sqlserver://localhost:1433;databaseName=" + dbName;
return url+serverName+":"+portNumber+";databaseName="+databaseName;
}

private java.sql.Connection getConnection(){
logger.info("--开始连接");
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) logger.info("--加载成功");
}catch(Exception e){
logger.info("--加载异常");
e.printStackTrace();
}
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){
logger.info("--已连接上,正在读取数据。。。");
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriver Name: "+ dm.getDriverName());
System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
System.out.println("\nDatabase Information ");
System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
System.out.println("Avalilable Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcatalog: "+ rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
}else {
logger.info("--获取连接失败");
} }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 main(String[] args) throws Exception
{
logger.info("--开始执行");
Connect myDbTest = new Connect();
myDbTest.displayDbProperties();
logger.info("--程序执行完毕");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值