JAVA连接SQLSERVER的例子
2008-01-05 18:48:11 来源:WEB开发网/*
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;
}
更多精彩
赞助商链接