Java - Mostrar SELECT de una tabla de MySQL en un JTable

 
Vista:
Imágen de perfil de Cesar Alexis

Mostrar SELECT de una tabla de MySQL en un JTable

Publicado por Cesar Alexis (1 intervención) el 05/08/2015 05:17:34
Quiero consultar una tabla de MySql
Materia se llama la tabla
y tiene 2 campos
id_materia int
nombre varchar
quiero ver los datos en un Jtable de Java
atrevez de una consulta SQL creo que es SELECT * FROM Materia
y solo se crear el JFrame y luego???
no se como hacer
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Mostrar SELECT de una tabla de MySQL en un JTable

Publicado por jack (3 intervenciones) el 08/08/2015 06:29:22
Mysql con tableview

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
 
public class StudentInfo {
 
 
    static String JDBC_DRIVER = "org.postgresql.Driver";
 
    //static String DB_URL = "jdbc:h2:file:C:/WAKILI/WAKILIdb";
    //  Database credentials
    static final String USER = "wrcobb";
    static final String PASS = "wrcobb";
 
    public static Connection conn = null;
 
    public List<KIWIDataModel> getAllstudentInfo() throws SQLException {
        Statement st = null;
        ResultSet rs;
        String driver = "org.postgresql.Driver";
 
        List ll = new LinkedList();
 
            String dbName = "wrcobb";
            String urlString = System.getProperty( "db.url", "jdbc:postgresql://localhost:5432/" +
                        dbName + "?user=wrcobb&password=wrcobb" );
            conn = DriverManager.getConnection( urlString );
            DatabaseMetaData dmd = conn.getMetaData();
        try {
            Class.forName(driver);
            //conn = DriverManager.getConnection(DB_URL, USER, PASS);
            conn = DriverManager.getConnection(urlString);
            st = conn.createStatement();
            String recordQuery = ("SELECT id, KIWI FROM KIWI");
 
            rs = st.executeQuery(recordQuery);
            while (rs.next()) {
                int Key = rs.getInt(1);
 
                ObservableList row = FXCollections.observableArrayList();
 
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    row.add(rs.getString(i));
                    System.out.println(row);
                }
 
                KIWIDataModel roww = new KIWIDataModel();
                roww.setFirstName(Key);
 
                ll.add(row);
 
            }
 
        } catch (ClassNotFoundException | SQLException ex) {
            // CATCH SOMETHING
        }
        return ll;
    }
}

CONTROLADOR CLASS:

i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
mport java.awt.EventQueue;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
 
/**
 *
 * @author administrator
 */
public class FXMLDocumentController implements Initializable {
 
    @FXML
    private Label label;
 
    @FXML
    private void handleEnterButtonAction(ActionEvent event) {
        System.out.println("You clicked Enter!");
        label.setText("Welcome");
        try
        {
            String driver = "org.postgresql.Driver";
            Class.forName( driver );
            String dbName = "wrcobb";
            String urlString = System.getProperty( "db.url", "jdbc:postgresql://localhost:5432/" +
                        dbName + "?user=wrcobb&password=wrcobb" );
            Connection conn = DriverManager.getConnection( urlString );
            DatabaseMetaData dmd = conn.getMetaData();
            ResultSet trs = dmd.getTables( null, null, "%",
                    new String[]{"TABLE"} );
            int tableCount = 0;
            while (trs.next()) {
                String tableName = trs.getString( "TABLE_NAME" );
                String typeName = trs.getString( "TABLE_TYPE" );
                System.out.printf( "Found %s, type: %s\n",
                        tableName, typeName );
                tableCount++;
            }
            trs.close();
            label.setText( String.format( "There are %d tables", tableCount ) );
        }
        catch( Exception ex )
        {
            label.setText( ex.toString() );
        }
    }
 
    @FXML
    private void handleExitButtonAction(ActionEvent event) {
        System.out.println("You clicked Exit!");
        label.setText("Thank You!");
        EventQueue.invokeLater( new Runnable() {
            public void run()
            {
                try {
                    Thread.sleep( 1000 );
                    System.exit( 0 );
                }
                catch( Exception ex )
                {
                    ex.printStackTrace();
                }
            }
        });
    }
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }
 
}

FXML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
 
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
 
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="studentinfo.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

Model Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import javafx.beans.property.SimpleIntegerProperty;
 
public class KIWIDataModel {
 
    public SimpleIntegerProperty firstName;
 
    public int getFirstName() {
        return firstName.get();
    }
 
    public void setFirstName(int fName) {
        firstName.set(fName);
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar