欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java连接sql server 2008数据库代码

程序员文章站 2023-12-09 21:52:33
java连接sqlserver 2008数据库的步骤: 1.到微软官方下载jdbc 并解压,得到sqljdbc.jar和sqljdbc4.jar,由于使用的是jdk1.7...

java连接sqlserver 2008数据库的步骤:

1.到微软官方下载jdbc 并解压,得到sqljdbc.jar和sqljdbc4.jar,由于使用的是jdk1.7,所以使用sqljdbc4.jar,

2.复制文件sqljdbc4.jar到jdk目录\jdk1.7.0\jre\lib\ext下。

配置系统变量classpath 变量路径 d:\java\jdk1.7.0\jre\lib\ext\sqljdbc4.jar

测试程序:

复制代码 代码如下:

 import java.sql.*;

 public class t1{

       public static void main(string []args)

     {                             

     try{                                                                    

      class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver");                                   

      system.out.println("成功加载sql驱动程序");                              

         }                              

    catch(exception e){

          system.out.println("找不到sql驱动程序");                                                            

          }                             

    system.out.println("hello huuuu!!");                         

   }

}


 

3.开始-〉程序-〉sql  server  2008-〉配置工具-〉sql server configuration manager。启动sql  2008服务。点击  sql  server2008网络配置节点,并选中”sqlserver的协议“节点。
启用tcp/ip协议,将ip地址中的ipall tcp端口设置为1433,然后到服务中重启 sqlserver(很重要啊。。。)。

测试程序:

复制代码 代码如下:

import java.sql.*; public class t2{

     public static void main(string []args)

     {                             

 try{                                                                    

        class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver");                                                                               system.out.println("成功加载sql驱动程序");                             

    }                               

   catch(exception e){

            system.out.println("找不到sql驱动程序");                                                             

         }                                                            

   try{                                                                    

       connectioncon=drivermanager.getconnection"jdbc:sqlserver://localhost:1433; 

              databasename=sqltest",         "sa","123");                                                                                                                                                                                                                                                                                       

 

        statement stmt = con.createstatement();                                                                                                                           system.out.println("数据库连接成功");                                  

     }                                 

    catch(exception e){                                                                                             

           system.out.println("数据库连接失败");                                                                                                                                                }                           

 }

}


注意上述程序中的databasename,需要先在数据库中创建名为sqltest的数据库。然后再开始连接。

好了,经过上述的配置和测试,以后就可以用java对数据库进行操作。