通过JDBC驱动,连接数据库;或者也可以通过某些第三方SQL驱动。

解决方案 »

  1.   


    try{
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();        
    String  url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=world-fortune";    
    //test为你的数据库的    
    String  user="sa";    
    String  password="45454545";    
    Connection  conn=  DriverManager.getConnection(url,user,password);        
          
            
    System.out.print("数据库操作成功,恭喜你"); 
             
    conn.close();        
    }
    catch(Exception e){
     out.print(e);
    }
    %>
      

  2.   

    微软网站下载SQL Server driver for JDBC连接到SQL Server数据库
    安装
    添加到classpath建立到一个SQL Server数据库的连接。
    Connection connection=null;
    try{
    String driverName=com.microsoft.jdbc.sqlserver.SQLServerDriver”; 
    String serverName=”local IP”   //Create connection
    String portNumber=”1433”
    String mydatabase=serverName+”:”+portNumber;
    String url=”jdbc: JSQLConnect://”+mydatabase;
    String username=”username”;
    String password=”password”;
    //Load the JDBC driver
    Class.forName(driverName);
    Connection=DriverManager.getConnection(url,username,password);
    }catch(ClassNotFoundException e){
    //Could not find database driver
    }catch(SQLException e){
    //Could not connect to the database
    }
      

  3.   

    Sorry,“String url=”jdbc: JSQLConnect://”+mydatabase;”改成:
    String  url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名";
      

  4.   

    最普通的可以用jdbc-odbc桥,只要配置odbc就可以连任何数据库。
    但一般建议用 专有驱动。。
    可以到微软的网站去下载。有详细的E文安装和使用说明的。。
      

  5.   

    我的代码是用JDBC桥连接的,
    这是从我自己的一个小程序中copy过来的,里面实现了一些功能
    你参考一下吧
    另用JDBC桥连接连接各种数据库的代码都是一样的,只要设置数据源就行了
    public class MyTimetable_Search {
      private String url;
      String find ="";
      String out = "";
      private Connection connect;
      private Statement statement;
      private ResultSet result;  public MyTimetable_Search(String aaaa) {
        MyFrame frame1 = new MyFrame();
        find = aaaa;
        try{
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          url = "jdbc:odbc:java-mysql";
          connect = DriverManager.getConnection(url,"","");
          statement = connect.createStatement();
          String query = "SELECT "+find+" FROM timetable";
          result = statement.executeQuery(query);
          while(result.next())
            {
            out = out+result.getString(1);
            }
          result.close();
          statement.close();
          connect.close();
          }
          catch(ClassNotFoundException cnfex){
            cnfex.printStackTrace();
          }
          catch(SQLException sqlex){
            sqlex.printStackTrace();
          }
      }
    }