-1
public class d4 extends JFrame implements ActionListener {

Connection con;
String dbName = "mydb";
String bdUser = "root";
String dbPassword = "2323";
String dbUrl = "jdbc:mysql://localhost/mydb";
JButton showButton;
static JLabel[] lbl;
JPanel panel;

public d4() {

    try {
        con = DriverManager.getConnection(dbUrl, bdUser, dbPassword);
        System.out.println("Connected to database successfully!");

    } catch (SQLException ex) {
        System.out.println("Could not connect to database");
    }

    add(mypanel(), BorderLayout.PAGE_START);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 500);
    setLocation(300, 30);
    setVisible(true);
}

public JPanel mypanel() {
    panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    showButton = new JButton("Show");
    showButton.addActionListener(this);
//        lbl = recordsLabel();
//        for (JLabel jlabel : lbl) {
//            panel.add(jlabel);               // Make no sense , Why?
//        }
    panel.add(showButton);

    return panel;
}

public static void main(String[] args) {
    new d4();
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == showButton) {
//
    }
}
    public JLabel[] recordsLabel() {
    try {
        Statement st1 = con.createStatement();
        ResultSet result1 = st1.executeQuery("select * from mytable");
        ArrayList<String> lableList = new ArrayList<>();
        while (result1.next()) {
            String resultRow = result1.getString(1) + " " + result1.getString(2);
            System.out.println(resultRow);
            lableList.add(resultRow);
        }
        Object[] arrayResultRow = lableList.toArray();

        int rows = result1.last() ? result1.getRow() : 0;
        System.out.println("Number of rows is: " + rows);

        lbl = new JLabel[rows];
        for (int i = 0; i < rows; i++) {
            lbl[i] = new JLabel(arrayResultRow[i].toString());
        }

    } catch (SQLException sqle) {
        System.out.println("Can not excute sql statement");
        sqle.printStackTrace();
    }
    return lbl;
}
}

输出:

Connected to database successfully!
10 sajjad
11 hamed
12 mehdi
13 hasan
555 fcvc
5858 cccc
1200 world
10 sajjad
1200 world
1200 world
1200 world
555 yes
333 ttt
1200 world
Number of rows is: 14
4

6 回答 6

2

Remove the return statement from inside the loop .The loop breaks once it encounters the return and gives back the caller of the method only the first record.

while (result1.next()) {
        System.out.println(result1.getString(1) + " " + result1.getString(2));
// instead of returning from here , you can create labels and set the text
// and return a List of labels.
        return result1.getString(1) + " "+ result1.getString(2); 
}

Loop through the resultset and populate some Collection and return the Collection at the end of the method once the loop ends. Also create labels , you have created only one label and setting the text of it from the return value of the showRecords2() method.

于 2013-07-04T15:52:31.940 回答
1

您不能多次读取每个 ResultSet 行。但是您可以存储结果值。

你可以试试

Statement st1 = con.createStatement();
ResultSet result1 = st1.executeQuery("select * from mytable");
Set<String> set = new HashSet<String>();
while (result1.next()) {
    String resultRow = result1.getString(1) + " " + result1.getString(2);
    System.out.println(resultRow);
    set.add(resultRow);
}
String[] resultRows = (String[])set.toArray();

int rows = result1.last() ? result1.getRow() : 0;
System.out.println("Number of rows is: " + rows);      // print 14 correctly!

for (int i = 0; i < rows; i++) {
    lbl = new JLabel[rows];
    lbl[i].setText(resultRows[i]);
}
于 2013-07-08T07:55:11.213 回答
1

您应该在循环内创建标签,而不是返回值,因为它会在第一行之后中断迭代

while (result1.next()) {
        System.out.println(result1.getString(1) + " " + result1.getString(2));

        // Create your label here, for the current text
}
于 2013-07-04T15:56:06.710 回答
1

这是没有任何例外的新版本。

class d4 extends JFrame implements ActionListener {

Connection con;
String dbName = "mydb";
String bdUser = "root";
String dbPassword = "2323";
String dbUrl = "jdbc:mysql://localhost/mydb";
JButton showButton;
static JLabel[] lbl;

public d4() throws ClassNotFoundException {

    Class.forName("com.mysql.jdbc.Driver");
    try {
        con = DriverManager.getConnection(dbUrl, bdUser,dbPassword);
        System.out.println("Connected to database successfully!");

    } catch (SQLException ex) {
        System.out.println("Could not connect to database");
    }

    add(mypanel(), BorderLayout.PAGE_START);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 500);
    setLocation(300, 30);
    setVisible(true);
}

public JPanel mypanel() {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    showButton = new JButton("Show");
    showButton.addActionListener(this);
    lbl = recordsLabel();
    for (JLabel jLabel : lbl) {
        panel.add(jLabel);

    }
    panel.add(showButton);

    return panel;
}

public static void main(String[] args) throws ClassNotFoundException {
    new d4();
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == showButton) {
        recordsLabel();     
        //     mypanel().add(recordsLabel());       // Error
    }
}

public JLabel[] recordsLabel() {
    try {
        Statement st1 = con.createStatement();
        ResultSet result1 = st1.executeQuery("select * from mytable");
        ArrayList<String> lableList = new ArrayList<String>();
        while (result1.next()) {
            String resultRow = result1.getString(1) + " " + result1.getString(2);
            System.out.println(resultRow);
            lableList.add(resultRow);
        }
        Object[] arrayResultRow = lableList.toArray();

        int rows = result1.last() ? result1.getRow() : 0;
        System.out.println("Number of rows is: " + rows);

        lbl = new JLabel[rows];
        for (int i = 0; i < rows; i++) {
            lbl[i] = new JLabel(arrayResultRow[i].toString());
        }

    } catch (Exception sqle) {
        System.out.println("Can not excute sql statement");
        sqle.printStackTrace();
        lbl=new JLabel[0];
    }
    return lbl;
}

}

于 2013-07-09T13:23:43.880 回答
1

Set 类中的 toArray 方法返回一个对象数组。您不能将 Object 数组转换为 String 数组。

试试这个代码:

public JLabel recordsLabel() {
    try {
        Statement st1 = con.createStatement();
        ResultSet result1 = st1.executeQuery("select * from mytable");
        Set<String> set = new HashSet<String>();
        while (result1.next()) {
            String resultRow = result1.getString(1) + " " + result1.getString(2);
            System.out.println(resultRow);
            set.add(resultRow);
        }
        Object[] arrayResultRow = set.toArray();

        System.out.println("Number of rows is: " + arrayResultRow.length);

        lbl = new JLabel[arrayResultRow.length];
        for (int i = 0; i < arrayResultRow.length; i++) {
             lbl[i]=new JLabel(arrayResultRow[i].toString());
        }

    } catch (SQLException sqle) {
        System.out.println("Can not excute sql statement");
        sqle.printStackTrace();
    }
    return null;
}

你可以这样做:

public JLabel recordsLabel() {
    try {
        Statement st1 = con.createStatement();
        ResultSet result1 = st1.executeQuery("select * from mytable");
        ArrayList<String> labelsList = new ArrayList<String>();
        while (result1.next()) {
            String resultRow = result1.getString(1) + " " + result1.getString(2);
            System.out.println(resultRow);
            labelsList.add(resultRow);
        }

        System.out.println("Number of rows is: " + labelsList.size());

        lbl = new JLabel[labelsList.size()];
        for (int i = 0; i < labelsList.size(); i++) {
            lbl[i]=new JLabel(labelsList.get(i));
        }

    } catch (SQLException sqle) {
        System.out.println("Can not excute sql statement");
        sqle.printStackTrace();
    }
    return null;
}
于 2013-07-08T20:55:27.417 回答
1

这很好用:

class d4 extends JFrame implements ActionListener {

Connection con;
String dbName = "mydb";
String bdUser = "root";
String dbPassword = "2323";
String dbUrl = "jdbc:mysql://localhost/onbook";
JButton showButton;
static JLabel[] lbl;
JPanel myPanel;

public d4() throws ClassNotFoundException {

    Class.forName("com.mysql.jdbc.Driver");
    try {
        con = DriverManager.getConnection(dbUrl, bdUser,dbPassword);
        System.out.println("Connected to database successfully!");

    } catch (SQLException ex) {
        System.out.println("Could not connect to database");
    }


     showButton = new JButton("Show");
    showButton.addActionListener(this);
    add(showButton,BorderLayout.PAGE_END);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 500);
    setLocation(300, 30);
    setVisible(true);
}

public JPanel mypanel() {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    lbl = recordsLabel();
    for (JLabel jLabel : lbl) {
        panel.add(jLabel);
    }
    return panel;
}

public static void main(String[] args) throws ClassNotFoundException {
    new d4();
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == showButton) {
        add(mypanel(), BorderLayout.PAGE_START); 
        setVisible(true);
        //     mypanel().add(recordsLabel());       // Error
    }

}

public JLabel[] recordsLabel() {
    try {
        Statement st1 = con.createStatement();
        ResultSet result1 = st1.executeQuery("select * from mytable");
        ArrayList<String> lableList = new ArrayList<String>();
        while (result1.next()) {
            String resultRow = result1.getString(1) + " " + result1.getString(2);
            System.out.println(resultRow);
            lableList.add(resultRow);
        }
        Object[] arrayResultRow = lableList.toArray();

        int rows = result1.last() ? result1.getRow() : 0;
        System.out.println("Number of rows is: " + rows);

        lbl = new JLabel[rows];
        for (int i = 0; i < rows; i++) {
            lbl[i] = new JLabel(arrayResultRow[i].toString());
        }

    } catch (Exception sqle) {
        System.out.println("Can not excute sql statement");
        sqle.printStackTrace();
        lbl=new JLabel[0];
    }
    return lbl;
}}
于 2013-07-10T08:17:30.193 回答