Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
hi I make this code to connect between java and sql server2005.
Java
 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
Posted
Updated 22-May-11 2:24am
v3
Comments
khad-2011 22-May-11 0:22am    
please help me
Tech Code Freak 7-Jun-11 12:57pm    
Just let everyone know whether you have got the solution and if Yes, then WHAT is IT?
If it is from one of these solutions, then just Accept them!

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.
 
Share this answer
 
Comments
khad-2011 22-May-11 12:48pm    
thanks
this is the output

Connection to principal server failed, trying the mirror server.
Connected to the new principal server.
No suitable driver found for jdbc:sqlserver://MyDBServer/SQLEXPRESS:1433;databaseName=SchPlan
Tech Code Freak 22-May-11 13:31pm    
I think that the sqljdbc driver is missing.
You can download it from
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=6d483869-816a-44cb-9787-a866235efc7c
Then use that driver to connect to the sql server.
If you are using Netbeans, then, in Servers tab, add new (the one you have downloaded from above link) driver to the drivers list, then configure it to connect to sql server.
Then you will get a list of tables in your database in your servers tab.
And after that, you will be able to connect to your server using above code.
If you are unable to connect to the server (while right-click on driver-->Connect using-->) then check in the sql server log on which port it is running. And as per that, modify the connection string.

Good Luck!
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.
Java
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 :)
 
Share this answer
 
v2
Comments
khad-2011 22-May-11 11:17am    
thanks
i did the first solution ,it cannot fix the error
<the second solution i did not understand it >
the third i fixed the server it is running good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900