zoukankan      html  css  js  c++  java
  • Java连接SQLServer 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。启动sql2008服务。点击  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{    
                                   Connection con=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对数据库进行操作。

  • 相关阅读:
    faster with MyISAM tables than with InnoDB or NDB tables
    w-BIG TABLE 1-toSMALLtable @-toMEMORY
    Indexing and Hashing
    MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size
    controlling the variance of request response times and not just worrying about maximizing queries per second
    Variance
    Population Mean
    12.162s 1805.867s
    situations where MyISAM will be faster than InnoDB
    1920.154s 0.309s 30817
  • 原文地址:https://www.cnblogs.com/sjlove/p/2942761.html
Copyright © 2011-2022 走看看