Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem with jdbc driver to connect Ms-SQL server 2005 (sqljdbc.jar 1.2)

807580Dec 8 2009 — edited Jun 12 2010
Hi,
I developed java application to connect Ms-SQL 2005.
When i execute it gets error.
What would be the reason of error?
Thanks.
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class sql2008_prog {
    
    /** Creates a new instance of sql2008_prog */
    public sql2008_prog() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://192.168.1.8\\SQLEXPRESS:1433;" +
                "database=TEST;user=sa;password=sa123456";
        
        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(connectionUrl);
            
            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT * FROM TABLO";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
            
            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        }
        
        // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        }
        
        finally {
            if (rs != null) try { rs.close(); } catch(Exception e) {}
            if (stmt != null) try { stmt.close(); } catch(Exception e) {}
            if (con != null) try { con.close(); } catch(Exception e) {}
        }  
    }
    
}
Error:

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot open database requested in login 'TEST'. Login fails.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at sql2008_prog.main(sql2008_prog.java:39)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 10 2010
Added on Dec 8 2009
7 comments
2,138 views