用sql sever2005连接Java [英] connect java with sql sever2005

查看:101
本文介绍了用sql sever2005连接Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我将这段代码连接到Java和sql server2005之间.

  import  java.awt.*;
导入 java.awt.event.*;
导入 javax.swing.*;
导入 javax.swing.table.*;
导入 java.io. *;
导入 java.net.*;
导入 java.util.*;
导入 java.text.*;
导入 java.sql.*;

公共  clientFailover {

   公共 静态  void  main(字符串 [] args){

      // 为连接字符串创建一个变量.
     字符串 connectionUrl = "  +
        " ;

      // 声明JDBC对象.
      连接con = null;
      语句stmt = null;

      尝试 {
         // 建立与主体服务器的连接.

        con = DriverManager.getConnection(connectionUrl);
     //  con = DriverManager.getConnection("jdbc:odbc:SchPlan"); 
         System.out.println(" );

         // 请注意,如果在此发生serverA的故障转移,则
         // 异常将被抛出,而故障转移伙伴将
         // 在下面的第一个catch块中使用.

         // 创建并执行一条插入某些数据的SQL语句.
      //  stmt = con.createStatement(); 

         // 请注意,以下语句假定
         //  TestTable表已在AdventureWorks中创建
         // 示例数据库.
      //  stmt.executeUpdate("INSERT INTO TestTable(Col2,Col3)VALUES('a',10)"); 
      }

      // 处理可能发生的任何错误.
      捕获(SQLException se){
         尝试 {
            // 与主体服务器的连接失败,
            // 试试现在可能是新的镜像服务器
            // 主体服务器.
            System.out.println("  +
            " );
         //  con = DriverManager.getConnection("jdbc:odbc:SchPlan"); 
            System.out.println(" );
           //  stmt = con.createStatement(); 
           //  stmt.executeUpdate("INSERT INTO TestTable(Col2,Col3)VALUES('a',10)"); 
         }
         捕获(异常e){
            e.printStackTrace();
         }
      }
      捕获(异常e){
         e.printStackTrace();
      }
      // 关闭JDBC对象.
      最终 {
         如果(stmt!= null) try  {stmt.close(); } 捕获(异常e){}
         如果(con!= null) try  {con.close(); } 捕获(异常e){}
      }
   }
} 


我将显示以下输出:
与主体服务器的连接失败,尝试使用镜像服务器.
已连接到新的主体服务器.

流程已完成.

请帮助我,错误在哪里

解决方案

SQL Server实例是否正在其他端口上运行?
我的曾经是1434年.
否则.
在SQLException的catch块中添加

System.out.println(se.getMessage());

,您将获得实际的错误消息.
在此处发布该消息,以便我为您提供帮助.


您正在问为什么与主体服务器的连接失败,对吗?

我先来看一下连接字符串.格式正确吗?不幸的是,我的Java太生锈了,无法发现任何问题.

也许您需要在数据库实例名称之前指定计算机名称?即

 字符串 connectionUrl = "  jdbc:sqlserver:// MyDBServer \  SQLEXPRESS:1433;" +
        " ; 


请参见构建连接URL [ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.util.*; import java.text.*; import java.sql.*; public class clientFailover { public static void main(String[] args) { // Create a variable for the connection string. String connectionUrl = "jdbc:sqlserver://SQLEXPRESS:1433;" + "databaseName=SchPlan;integratedSecurity=true;" ; // Declare the JDBC objects. Connection con = null; Statement stmt = null; try { // Establish the connection to the principal server. con = DriverManager.getConnection(connectionUrl); // con = DriverManager.getConnection("jdbc:odbc:SchPlan"); System.out.println("Connected to the principal server."); // Note that if a failover of serverA occurs here, then an // exception will be thrown and the failover partner will // be used in the first catch block below. // Create and execute an SQL statement that inserts some data. // stmt = con.createStatement(); // Note that the following statement assumes that the // TestTable table has been created in the AdventureWorks // sample database. // stmt.executeUpdate("INSERT INTO TestTable (Col2, Col3) VALUES ('a', 10)"); } // Handle any errors that may have occurred. catch (SQLException se) { try { // The connection to the principal server failed, // try the mirror server which may now be the new // principal server. System.out.println("Connection to principal server failed, " + "trying the mirror server."); // con = DriverManager.getConnection("jdbc:odbc:SchPlan"); System.out.println("Connected to the new principal server."); // stmt = con.createStatement(); // stmt.executeUpdate("INSERT INTO TestTable (Col2, Col3) VALUES ('a', 10)"); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } // Close the JDBC objects. finally { if (stmt != null) try { stmt.close(); } catch(Exception e) {} if (con != null) try { con.close(); } catch(Exception e) {} } } }


I take this output:
Connection to principal server failed, trying the mirror server.
Connected to the new principal server.

Process completed.

please help me where are the error

May be the SQL Server instance is running on some other port?
Mine was on 1434 once.
or else..
Add

System.out.println(se.getMessage());

in the catch block of SQLException, and you will get the actual error message.
Post that message here so that I can help you.


You''re asking why the connection to the principal server fails, right?

I''d start with taking a look at the connection string. Is it correct and in the right format? Unfortunately my Java is too rusty to be able to spot any problems.

EDIT: maybe you need to specify a machine name before the database instance name? I.e.

String connectionUrl = "jdbc:sqlserver://MyDBServer\SQLEXPRESS:1433;" +
        "databaseName=SchPlan;integratedSecurity=true;" ;


See
Building the Connection URL[^]

Secondly, have a look at what the SQLException you''re getting is telling you. Add a System.out.println statement into the catch block for the SQL exception, and print its contents.

Thirdly, make sure the principal server is actually running :)


这篇关于用sql sever2005连接Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆