0

有人可以通过使用向量创建数据库查询来帮助我吗?

我想查询数据库的Lieferrantennordwind并将其显示在JTable.

我的问题是如何在jTable1?

这是我之前的代码:

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.event.*;
 import javax.swing.table.*;
 import java.sql.*;
 import java.util.*;



  public class Anzeige extends JFrame {




  private JTable jTable1 = new JTable(5, 5);
  private DefaultTableModel jTable1Model = (DefaultTableModel) jTable1.getModel();
  private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);



 public Anzeige (String title) {
  super (title);
  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  int frameWidth = 676; 
  int frameHeight = 467;
  setSize(frameWidth, frameHeight);
  Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  int x = (d.width - getSize().width) / 2;
  int y = (d.height - getSize().height) / 2;
  setLocation(x, y);
  Container cp = getContentPane();
  cp.setLayout(null);


  // Anfang Komponenten
  jTable1ScrollPane.setBounds(72, 56, 521, 289);
  jTable1.getColumnModel().getColumn(0).setHeaderValue("Title 1");
  jTable1.getColumnModel().getColumn(1).setHeaderValue("Title 2");
  jTable1.getColumnModel().getColumn(2).setHeaderValue("Title 3");
  jTable1.getColumnModel().getColumn(3).setHeaderValue("Title 4");
  jTable1.getColumnModel().getColumn(4).setHeaderValue("Title 5");
  cp.add(jTable1ScrollPane);
  // Ende Komponenten
  setResizable(false);
  setVisible(true);



 }



public static void main(String[] args) {
new Anzeige("Anzeige");



   try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();

    Connection dbConn =    DriverManager.getConnection("jdbc:mysql://localhost:3306/nordwind", "root", "");

  Statement statement = dbConn.createStatement();

  ResultSet results = statement.executeQuery("SELECT * FROM Lieferranten");

  Vector vector = new Vector();



  while (results.next()) {
    String s1 = results.getString(2);
    String s2 = results.getString(5);
    System.out.println(s1 + "\n" + s2);
    System.out.println(results.getString(2) + " " + results.getString(5));

    Vector data = new Vector();
    data.add(results.getString(1));
    data.add(results.getString(2));
    vector.add(data);


  }
  //results.close();
  statement.close();
  dbConn.close();


}
catch (InstantiationException e) {
  System.err.println("Error in Instantiation!");
}

catch (ClassNotFoundException e) {
  System.err.println("Class not found!");
}

catch (IllegalAccessException e) {
  System.err.println("Access denied!");
}


catch (SQLException e) {
  System.err.println("SQL Error!");
}


}

}
4

1 回答 1

0

我假设您将获得五个列值,因为您添加了五个列标题。之后使用addRow(java.util.Vector)方法DefaultTableModeljTable1.

import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.event.*;
 import javax.swing.table.*;
 import java.sql.*;
 import java.util.*;



  public class Anzeige extends JFrame {




  private JTable jTable1 = new JTable(5, 5);
  private DefaultTableModel jTable1Model = (DefaultTableModel) jTable1.getModel();
  private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);



 public Anzeige (String title) {
  super (title);
  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  int frameWidth = 676; 
  int frameHeight = 467;
  setSize(frameWidth, frameHeight);
  Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  int x = (d.width - getSize().width) / 2;
  int y = (d.height - getSize().height) / 2;
  setLocation(x, y);
  Container cp = getContentPane();
  cp.setLayout(null);


  // Anfang Komponenten
  jTable1ScrollPane.setBounds(72, 56, 521, 289);
  jTable1.getColumnModel().getColumn(0).setHeaderValue("Title 1");
  jTable1.getColumnModel().getColumn(1).setHeaderValue("Title 2");
  jTable1.getColumnModel().getColumn(2).setHeaderValue("Title 3");
  jTable1.getColumnModel().getColumn(3).setHeaderValue("Title 4");
  jTable1.getColumnModel().getColumn(4).setHeaderValue("Title 5");
  cp.add(jTable1ScrollPane);
  // Ende Komponenten
  setResizable(false);
  setVisible(true);



 }



public static void main(String[] args) {
new Anzeige("Anzeige");



   try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();

    Connection dbConn =    DriverManager.getConnection("jdbc:mysql://localhost:3306/nordwind", "root", "");

  Statement statement = dbConn.createStatement();

  ResultSet results = statement.executeQuery("SELECT * FROM Lieferranten");


  while (results.next()) {
    String s1 = results.getString(2);
    String s2 = results.getString(5);
    System.out.println(s1 + "\n" + s2);
    System.out.println(results.getString(2) + " " + results.getString(5));

    Vector data = new Vector();
    data.add(results.getString(1));
    data.add(results.getString(2));
    data.add(results.getString(3));
    data.add(results.getString(4));
    data.add(results.getString(5));
    jTable1Model.addRow(data);


  }
  //results.close();
  statement.close();
  dbConn.close();


}
catch (InstantiationException e) {
  System.err.println("Error in Instantiation!");
}

catch (ClassNotFoundException e) {
  System.err.println("Class not found!");
}

catch (IllegalAccessException e) {
  System.err.println("Access denied!");
}


catch (SQLException e) {
  System.err.println("SQL Error!");
}


}

}
于 2013-03-10T14:01:53.393 回答