-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

returnループ内からステートメントを削除します。ループは に遭遇するreturnと中断し、メソッドの呼び出し元に最初のレコードのみを返します。

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); 
}

をループしてresultsetCollection にデータを入力し、ループが終了したらメソッドの最後で Collection を返します。また、ラベルを作成します。ラベルを 1 つだけ作成し、showRecords2()メソッドの戻り値からそのテキストを設定しました。

于 2013-07-04T15:52:31.940 に答える
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/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 に答える
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

これは例外なく動作する新しいバージョンです。

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 に答える