亲宝软件园·资讯

展开

java连接sql server 2008数据库 java连接sql server 2008数据库代码

人气:0
想了解java连接sql server 2008数据库代码的相关内容吗,在本文为您仔细讲解java连接sql server 2008数据库的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,sql2008,下面大家一起来学习吧。

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对数据库进行操作。

加载全部内容

相关教程
猜你喜欢
用户评论