2
  import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.sql.*;
import java.net.*;

public class connection {
    JTextField textfeild;
    JButton button;
    String text;
    Socket sock;
    PrintWriter writer;
    JButton button1;

    public static void main(String[] args) {
        connection user1 = new connection();
        user1.go();
    }//main method close

    public void go() {
        JFrame frame12 = new JFrame();
        JPanel centerpanel12 = new JPanel();
        centerpanel12.setLayout(new BoxLayout(centerpanel12, BoxLayout.Y_AXIS));
        textfeild = new JTextField(20);
        centerpanel12.add(textfeild);
        //textfeild.addActionListener(new textfeildlitner());
        frame12.add(centerpanel12);
        button = new JButton("Click Me");
        centerpanel12.add(button);
        button.addActionListener(new buttonlitner());
        button1 = new JButton("DataDisplay");
        centerpanel12.add(button1);
        button1.addActionListener(new buttonlitner1());

        frame12.getContentPane().add(BorderLayout.CENTER, centerpanel12);
        frame12.pack();
        frame12.setVisible(true);
        frame12.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }//go method close

      /*class textfeildlitner implements ActionListener{
          public void actionPerformed(ActionEvent ev){

          }
      }//inner class textfeildlitner close*/

    class buttonlitner implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            button.setText("I AM Clicked");
            String name = textfeild.getText();
            System.out.println(name);
            textfeild.setText("");
            textfeild.requestFocus();
        }//method close
    }//inner class close


    class buttonlitner1 implements ActionListener {
        void connection() {
            try {
                String user = "SQlUI";
                String pass = "123456";
                String db = "jdbc:sqlserver://localhost:1234;" + ";databaseName=SQlUI";
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Connection con = DriverManager.getConnection(db, user, pass);
                Statement s1 = con.createStatement();
                ResultSet r1 = s1.executeQuery("select * from Table_1");
                String[] result = new String[20];
                if (r1 != null) {
                    while (r1.next()) {
                        for (int i = 0; i < result.length; i++) {
                            for (int j = 0; j < result.length; j++) {
                                result[j] = r1.getString(i);
                                System.out.println(result[j]);
                                break;
                            }//for j close
                        }//for i close
                    }//if closeclose
                }//try close
            } catch (Exception ex) {
                ex.printStackTrace();
            }//catch close
        }//connection() close     

        public void actionPerformed(ActionEvent ev) {
            button1.setText("Processing");
            new buttonlitner1().connection();
        }//method close
    }//inner class close
}//outer class close

And the exception which i am getting for this code-

com.microsoft.sqlserver.jdbc.SQLServerException: The index 0 is out of range.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(SQLServerResultSet.java:531)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(SQLServerResultSet.java:2049)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2082)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2067)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(SQLServerResultSet.java:2392)
    at connection$buttonlitner1.connection(connection.java:76)
    at connection$buttonlitner1.actionPerformed(connection.java:89)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

When i am changing the value of i from 0 to 1 then it displays result for 1st row but again it throws exception saying that the index 3 is out of range. I have posted the same code and after making the changes (as suggested by the experts on stackoverflow) it gave me a new type of exception. I am putting the link of the previous question-Getting Checked Exception while running a SQL Program and the question was Getting Checked Exception while running a SQL Program

8
  • 1
    how many columns has in Table_1? Commented May 4, 2014 at 9:24
  • 1
    @lakhman Table_1 has 2 colums and 9 rows Commented May 4, 2014 at 10:36
  • 1
    then your outer loop should be for (int i = 1; i < 3; i++). since your result array has 20 length, it tries to access 20 columns from table but you have 2 column table. that's why you get index out of bound excetption Commented May 4, 2014 at 13:04
  • @lakshman Thanks a ton sir because of u i am getting the output without exception. As u can understand i am a beginner in java. So could u please give me some suggestion for writing java codes Commented May 4, 2014 at 13:09
  • glad if that help.:). I am also not an expert. but I can say you can try to answer some stackoverflow questions.that will improve you greatly..:) Commented May 4, 2014 at 13:45

1 Answer 1

5

In SQL (unlike generally in Java) evrything is indexed starting from 1 - both rows and columns.

Why do you have 2 loops inside your r1.next() loop?

Think what your code

for (int j = 0; j < result.length; j++) {
    result[j] = r1.getString(i);
    System.out.println(result[j]);
    break;
}

actually does.

Sign up to request clarification or add additional context in comments.

1 Comment

Sir actually i was assigning wrong condition for my outerloop thats why i was getting those exceptions. Now answer to your question is-first for loop takes column from where i am trying to get data and the second loop stores the data and finally print it. In this first loop for (int i = 0; i < result.length; i++){} i was implementing wrong condition but i replaced result.length by the column number and now the same code is working fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.