问题描述:

Does someone can help my by creating an database query with vectors?

I want to query the Lieferranten Table of the nordwind database and showing it on a JTable.

My problem is how to show data sets in the jTable1?

This is my previous code:

 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!");

}

}

}

网友答案:

I assume that you will get five column values as you had added five column titles. After that use addRow(java.util.Vector) method of DefaultTableModel to insert row in 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");


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


}

}
相关阅读:
Top