Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String index out of range: -10

Status
Not open for further replies.

jem122974

Programmer
Nov 1, 2001
114
US
I understand that I'll get something like this if I tried a substring with an index greater than the length of the string. But in this case I'm not doing anything like that. Also what does the negative number mean?

I'm getting "Open DB:String index out of range: -10"

Here is my code...

Code:
        try {
	    //
	    // Connect to SQL Server
	    //
        	conn = new SQLServerConn(server, port, username, password);
            stmt = conn.SQLServerConnection().createStatement();
        }
        catch (Exception e) {
        	spResultText="Open DB:" + e.getMessage();
                set(7,spResultText);
        	return;
        }

Here is the SQLServerConn code:

Code:
    public SQLServerConn() throws Exception {
    	connect(null, null, null, null);
    }
    
    public SQLServerConn(String server, String port, String username, String password) throws Exception {
    	connect(server, port, username, password);
    }
    
    private void connect(String srv, String pt, String usr, String pwd) throws Exception {

        String port;         // Port SQL Server is listening on
        String host;         // SQL Server host name or IP Address
        String sqlUser;      // SQL Server user name
        String sqlPwd;       // SQL Server password
        String iSeriesJob;   // iSeries Job Name
        //
        // If connection already there,
        // (and not closed) bail out!
        //
        if (connSQLServer!=null) {
            if(!connSQLServer.isClosed()) {
                return;
            }
        }


        // initialize the JDBC driver class
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        // Connection properties are stored in /MAINSAVER folder
        if (srv == null) {
	        ConnSettings msSettings=new ConnSettings("/mainsaver/SQLProperties.dat");
	        port=msSettings.getProperty("Port");
	        host=msSettings.getProperty("Host");
	        sqlUser=msSettings.getProperty("User");
	        sqlPwd=msSettings.getProperty("Password");
        }
        else {
        	port = pt;
        	host = srv;
        	sqlUser = usr;
        	sqlPwd = pwd;
        }

        try {
            iSeriesJob=MSRPGNative.iSeriesJobName();
        } catch(Exception e) {
            iSeriesJob="SQLServer (unknown)";
        }

        String hostName;     // Get Name of AS/400
        hostName=InetAddress.getLocalHost().getHostName();

        try {
            connSQLServer = DriverManager.getConnection("jdbc:sqlserver:" +
                                                        "//"+host+":"+port+";" +
                                                        "User="+sqlUser+";"+
                                                        "Password="+sqlPwd+";"+
                                                        "ProgramName=SqlServer("+hostName+");"+
                                                        "Wsid="+iSeriesJob+";");
        }
        catch (Exception e) {
            // Truncate unnecessary JDBC Message from SQL Server
            // [SQL Server JDBC Driver], etc.
            throw new Exception(e.getMessage().substring(43));
        }
    }
 
Silly me... At the very end I am doing a substring after all! And that was the problem... We were making an assumption that all the SQL Server errors were prefixed with some jargon. That was true while we used the SQL Server 2000 driver, but now that we are using the SQL Server 2005 driver it is not. So problem solved...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top