经典jdbc连接sql server实例(微软官方网站上的)
2005-04-26 12:44
477 查看
import java.*;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "127.0.0.1";
private final String portNumber = "1433";
private final String databaseName= "mtv";
private final String userName = "sa";
private final String password = "123456";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
// private final String selectMethod = "cursor";
// Constructor
public Connect(){}
private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";";
}
private java.sql.Connection getConnection(){
try{
Class.forName("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("/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 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 main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperties();
}
}
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "127.0.0.1";
private final String portNumber = "1433";
private final String databaseName= "mtv";
private final String userName = "sa";
private final String password = "123456";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
// private final String selectMethod = "cursor";
// Constructor
public Connect(){}
private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";";
}
private java.sql.Connection getConnection(){
try{
Class.forName("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("/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 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 main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperties();
}
}
相关文章推荐
- JDBC 连接 带实例名的SQL Server
- jdbc根据实例名 连接 sql server
- 使用JDBC连接SQL Server 2000数据库实例讲解
- JDBC 连接 带实例名的SQL Server
- 使用JDBC连接SQL Server 2000 命名实例(Named Instance)
- 在微软的官方网站上看到的--SQL Server 本机 Web服务的使用方案!
- ASP的网站配置:请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
- Microsoft SQL Server 2000 Driver for JDBC SP1与连接SQL Server实例的问题
- SQL Server 2008 下载地址(微软官方网站)
- JDBC 连接 带实例名的SQL Server
- JAVA之使用JDBC连接数据库-使用微软sqljdbc连接SQL Server 2005数据库
- JDBC连接SQL Server 2005总结
- 在与SQL Server建立连接时出现与网络相关的或特定于实例的错误
- jdbc 连接sql server 2000的问题
- Java/JSP中使用JDBC连接SQL Server 2005~
- JDBC 连接 Sql Server 数据 报错
- 快速搞定在Eclipse中用JDBC连接SQL Server 2005
- kettle 通过JDBC 连接SQL Server(Error occurred while trying to connect to the database)
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器
- 微软推出SQL Server 2005 JDBC Driver 1.1