首页 > 学院 > 开发设计 > 正文

JAVA连接SQLSERVER的例子

2019-11-18 11:21:30
字体:
来源:转载
供稿:网友

/*
This source for connect Microfost SQL Server 2000 and test servic infomation
This source is modify by GongTao
http://support.microsoft.com/default.aspx?scid=kb;en-us;313100
set classpath=.;C:/Myjava/JDBCPack/MSSQL/msbase.jar;C:/MyJava/JDBCPack/MSSQL/msutil.jar;C:/MyJava/JDBCPack/MSSQL/mssqlserver.jar

*/
import java.sql.*;
import java.io.*;

import java.*;
public class Connect{
     PRivate java.sql.Connection  con = null;
     private final String url = "jdbc:microsoft:sqlserver://";
     private final String serverName= "GONGTAO";
     private final String portNumber = "1433";
     private final String databaseName= "pubs";
     private final String userName = "sa";
     private final String passWord = "";
     // 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+";selectMethod="+selectMethod+";";
     }
    
     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;
      }



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表