Hallo,
ich möchte aus einer SQL Abfrage die Daten in einem Swing Objekt anzeigen lassen und habe dazu bis jetzt folgenden Code:
Gerade die Übergabe der Daten aus einer Abfrage der SQL Datenbank in ein Swing Objekt macht mir Probleme....!
ich möchte aus einer SQL Abfrage die Daten in einem Swing Objekt anzeigen lassen und habe dazu bis jetzt folgenden Code:
Code:
package components; /* * SimpleTableDemo.java requires no other files. */ import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; public class QueryDBtest extends JPanel { private boolean DEBUG = false; JTextField testfield; // Textfeld /** * Runs a test by creating a table, adding a value, showing the table contents, and * removing the table. **/ public static void runTest() throws SQLException, IOException { Connection conn = getConnection(); try { Statement stat = conn.createStatement(); ResultSet result = stat.executeQuery("SHOW TABLES"); int count = 0; while (result.next()) count++; System.out.println("Es gibt " + count + " Tabellen."); result.close(); String query = "SELECT * FROM gratulation"; ResultSet res = stat.executeQuery(query); while(res.next()) { System.out.println(res.getString(1)); } res.close(); } finally { conn.close(); } } public QueryDBtest() { super(new GridLayout(1,0)); String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; Object[][] data = { {"Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false)}, {"John", "Doe", "Rowing", new Integer(3), new Boolean(true)}, {"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)}, {"Jane", "White", "Speed reading", new Integer(20), new Boolean(true)}, {"Joe", "Brown", "Pool", new Integer(10), new Boolean(false)} }; testfield = new JTextField(); testfield.setVisible(true); testfield.setText("Hallo"); testfield.setSize(50, 500); testfield.setBackground(Color.cyan); final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 150)); table.setFillsViewportHeight(true); if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } table.add(testfield); //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); } private void printDebugData(JTable table) { int numRows = table.getRowCount(); int numCols = table.getColumnCount(); javax.swing.table.TableModel model = table.getModel(); System.out.println("Value of data: "); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + model.getValueAt(i, j)); } System.out.println(); } System.out.println("--------------------------"); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("SimpleTableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. QueryDBtest newContentPane = new QueryDBtest(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); try { runTest(); } catch (SQLException ex) { for (Throwable t: ex) t.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } /** * Gets a connection from the properties specified in the file database properties * @return the database connection **/ public static Connection getConnection() throws SQLException, IOException { String drivers = "com.mysql.jdbc.Driver"; if(drivers != null) System.setProperty("jdbc.drivers", drivers); String url = "jdbc:mysql://localhost:3306/corejava"; String username = "Benutzer"; String password = "Passwort"; return DriverManager.getConnection(url, username, password); } }
Comment