您的位置:首页 >> 编程开发 >> Java >> 数据库 >> 正文
数据库 RSS
 

JAVA连接SQLSERVER的例子

http://www.rdxx.com 02年11月09日 10:42 互联网 我要投稿

关键词: SQLServer , Java , 连接 , SQL

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

     /*
  
          Display the driver properties, database details
     */

     public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;

9 7 3 1 2 3 4 8 :


 
 
标签: SQLServer , Java , 连接 , SQL 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站