java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java连接sql server 2008数据库

java连接sql server 2008数据库代码

作者:

Java的学习,很重要的一点是对数据库进行操作。

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

您可能感兴趣的文章:
阅读全文