WEB开发网
开发学院WEB开发Jsp JAVA连接SQLSERVER的例子 阅读

JAVA连接SQLSERVER的例子

 2008-01-05 18:48:11 来源:WEB开发网   
核心提示:/*This source for connect Microfost SQL Server 2000 and test servic infomationThis source is modify by GongTaohttp://support.microsoft.com/default.aspx?scid=kb;

/*
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;
   }


Tags:JAVA 连接 SQLSERVER

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接