Problems filling JTABLE with databasedata

M

Maenne

Hi together,

I'm working with eclipse 3.4.1, java 2 jdk 1.4.2.01 and jigloo for GUI.

Maybe someone can help me.
First I'd build a GUI with jigloo. (With a JTABLE). Then I created a
JDBC-ODBC connection to a MSAccess database. So far, so good.

Now my problem:
I have no idea, how to connect the JTABLE with the data from the
database.
I know, how to create the first column, but how to place the WHILE-statement,
so that i get all the data?

Here is my sourcecode:
private JTable getAccesstab() throws SQLException {

if(Accesstab == null) {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:eek:dbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Testdaten.mdb");

Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet tbladressen = stmt2.executeQuery("SELECT * FROM [Adressen];");

ResultSetMetaData rsmd = tbladressen.getMetaData();

int clmCnt = rsmd.getColumnCount();

String[][] items1 = new String[clmCnt],new String[] { "Vorname", "Nachname" };

tbladressen.first();

while (tbladressen.next()) {

for (int i = 1; i <= clmCnt; i++) {

items1=tbladressen.getString(i);

}

tbladressen.next();

}


TableModel AccesstabModel =

new DefaultTableModel(

new String[][] { items1 },

new String[] { "Vorname", "Nachname" });




// new String[][] { { tbladressen.getString(3), tbladressen.getString(4) },

// { "Three", "Four" },

// { "Three", "Four" } },

// new String[] { "Vorname", "Nachname" });

Accesstab = new JTable();

GroupLayout AccesstabLayout = new GroupLayout((JComponent)Accesstab);

Accesstab.setLayout(AccesstabLayout);

AccesstabLayout.setHorizontalGroup(AccesstabLayout.createParallelGroup());

AccesstabLayout.setVerticalGroup(AccesstabLayout.createParallelGroup());

Accesstab.setModel(AccesstabModel);


tbladressen.close();

con.close();




} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}




}

return Accesstab;

}



thanks for your help

Maenne
 
J

John B. Matthews

"Maenne said:
I'm working with eclipse 3.4.1, java 2 jdk 1.4.2.01 and jigloo for
GUI.

Maybe someone can help me. First I'd build a GUI with jigloo. (With a
JTABLE). Then I created a JDBC-ODBC connection to a MSAccess
database. So far, so good.

Now my problem: I have no idea, how to connect the JTABLE with the
data from the database. I know, how to create the first column, but
how to place the WHILE-statement, so that i get all the data?
[...]

I don't know jigloo, MSAccess or ODBC, but you might want to look at a
simpler example to get started, perhaps something in a tutorial:

<http://java.sun.com/docs/books/tutorial/jdbc/index.html>

Your SDK may include developer examples about using JTable with JDBC; on
my platform, it's found in Examples/Java/JFC/TableExample. Also, you
might look in the corresponding tutorial:

<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>
 
R

RedGrittyBrick

Maenne said:
Hi together,

I'm working with eclipse 3.4.1, java 2 jdk 1.4.2.01 and jigloo for GUI.

Maybe someone can help me.

I'd love to, but you are placing many obstacles in my way.

First I'd build a GUI with jigloo. (With a JTABLE). Then I created a
JDBC-ODBC connection to a MSAccess database. So far, so good.

JDBC-ODBC has a bad reputation. MS Access as a database has a poor
reputation. However I guess it should work despite that.

Now my problem:
I have no idea, how to connect the JTABLE with the data from the
database.

Yet you seem to have had the idea of using JDBC, I think that is a
reasonable way to start.

I know, how to create the first column, but how to place the WHILE-statement,
so that i get all the data?

I tend to do

while (resultSet.next()) {
// create a data transfer object e.g. "Address"
// add it to a data structure, e.g. List<Address>
}
// use the List said:
Here is my sourcecode:

You did not include the whole of your source code. I'd have to load it
into an IDE, reformat it, and spend time filling in the parts you have
omitted. Combined with the lack of indentation this somewhat deflates my
enthusiasm.

Please read http://sscce.org/

private JTable getAccesstab() throws SQLException {

if(Accesstab == null) {

try {
....

thanks for your help

Please indent the code with spaces (not tabs) - you can get your IDE (or
editor) to do that for you.

Did you catch SQLExceptions anywhere in there?

What error messages did you get when you compiled this code?
What error messages did you get when you ran this code?
 
M

Maenne

Hi John,

my problem is not to fill the JTABLE generally, that's not a problem.
My problem is that I fetched the data from the database.


Afterwards I have to fill that array bellow with this fetched data.

tableadressen.first
DO While tableadressen not EOF
new String[][] { { tbladressen.getString(3), tbladressen.getString(4) }, //fill with the data from database
tableadressen.next
}
new String[] { "Vorname", "Nachname" });

In the examples I read is only told about static values, which war entered in the sourcecode, but no database data.

Thanks Maenne


John B. Matthews said:
Maenne said:
I'm working with eclipse 3.4.1, java 2 jdk 1.4.2.01 and jigloo for
GUI.

Maybe someone can help me. First I'd build a GUI with jigloo. (With a
JTABLE). Then I created a JDBC-ODBC connection to a MSAccess
database. So far, so good.

Now my problem: I have no idea, how to connect the JTABLE with the
data from the database. I know, how to create the first column, but
how to place the WHILE-statement, so that i get all the data?
[...]

I don't know jigloo, MSAccess or ODBC, but you might want to look at a
simpler example to get started, perhaps something in a tutorial:

<http://java.sun.com/docs/books/tutorial/jdbc/index.html>

Your SDK may include developer examples about using JTable with JDBC; on
my platform, it's found in Examples/Java/JFC/TableExample. Also, you
might look in the corresponding tutorial:

<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>
 
M

Maenne

Hi RedGrittyBrick,

at first, thank you for your answer. You're right, when you say, that MS Access is a poor Database, but in that case
it's the easiest way for me to get a connection to a database, by the way to learn java. Later, I try to use other db's.
No I got no SQL-Execptions. The fetching of the database data was successfully.

Here the complete coding without tabs:

package gui;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import javax.swing.SwingUtilities;
//Datenbank
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
private JMenuBar jMenuBar1;
private JMenu jMenu1;
private JMenu helpMenu;
private JMenuItem jMenuItem1;
private JScrollPane jScrollPane1;
private JTable Accesstab;
private JTextField Vorname;
private JTextField Nachname;
private static JTextField Eingabe;
private AbstractAction closeAboutAction;
private JButton jButton1;
private JLabel jLabel1;
private JDialog jDialog1;
private AbstractAction aboutAction;



/**
* Auto-generated main method to display this JFrame
* @throws Exception
*/
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);

}
});

}

public NewJFrame() {
super();

initGUI();

}




private void initGUI() {
try {

// dbload1();



GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
getContentPane().setLayout(thisLayout);
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
.addContainerGap(12, 12)
.add(getJScrollPane1(), GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
.add(59)
.add(getVorname(), GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.UNRELATED)
.add(getNachname(), GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addContainerGap(28, 28));
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(80, 80)
.add(getJScrollPane1(), GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.add(67)
.add(thisLayout.createParallelGroup()
.add(GroupLayout.BASELINE, getNachname(), GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.add(GroupLayout.BASELINE, getVorname(), GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
.addContainerGap(12, 12));
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);



this.setTitle("Hauptmenü");
{
jMenuBar1 = new JMenuBar();
setJMenuBar(jMenuBar1);
{
jMenu1 = new JMenu();
jMenuBar1.add(jMenu1);
jMenu1.setText("File");
}
{
helpMenu = new JMenu();
jMenuBar1.add(helpMenu);
helpMenu.setText("Help");
{
jMenuItem1 = new JMenuItem();
helpMenu.add(jMenuItem1);
jMenuItem1.setText("jMenuItem1");
jMenuItem1.setAction(getAboutAction());
}
}
}
pack();
this.setSize(400, 321);
} catch (Exception e) {
e.printStackTrace();
}
}

private AbstractAction getAboutAction() {
if(aboutAction == null) {
aboutAction = new AbstractAction("About", null) {
public void actionPerformed(ActionEvent evt) {
getJDialog1().pack();
getJDialog1().setLocationRelativeTo(null);
getJDialog1().setVisible(true);
try {
dbload1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
return aboutAction;
}

private JDialog getJDialog1() {
if(jDialog1 == null) {
jDialog1 = new JDialog(this);
GroupLayout jDialog1Layout = new GroupLayout((JComponent)jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1.setTitle("About");
{
jLabel1 = new JLabel();
jLabel1.setText("Hallo Sie");
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setFont(new java.awt.Font("Tahoma",1,16));
}
{
jButton1 = new JButton();
jButton1.setText("OK");
jButton1.setAction(getCloseAboutAction());
}
jDialog1Layout.setVerticalGroup(jDialog1Layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(getEingabe(), GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.add(148, 148, GroupLayout.PREFERRED_SIZE)
.add(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap());
jDialog1Layout.setHorizontalGroup(jDialog1Layout.createSequentialGroup()
.addContainerGap()
.add(jDialog1Layout.createParallelGroup()
.add(GroupLayout.LEADING, jDialog1Layout.createSequentialGroup()
.add(jLabel1, GroupLayout.PREFERRED_SIZE, 209, GroupLayout.PREFERRED_SIZE)
.add(0, 104, Short.MAX_VALUE)
.add(jButton1, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
.add(jDialog1Layout.createSequentialGroup()
.add(131, 131, GroupLayout.PREFERRED_SIZE)
.add(getEingabe(), GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
.add(17)))
.addContainerGap());
}
return jDialog1;
}

private AbstractAction getCloseAboutAction() {
if(closeAboutAction == null) {
closeAboutAction = new AbstractAction("OK", null) {
public void actionPerformed(ActionEvent evt) {
getJDialog1().dispose();
}
};
}
return closeAboutAction;
}
public static void dbload1() throws Exception {

}

private JTextField getEingabe() {
if(Eingabe == null) {
Eingabe = new JTextField();



}


return Eingabe;
}

private JTextField getVorname() throws SQLException {
if(Vorname == null) {
Vorname = new JTextField();
//Vorname.setText("Vorname");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:eek:dbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Testdaten.mdb");
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet tbladressen = stmt2.executeQuery("SELECT * FROM [Adressen] WHERE [Vorname]='Vinzenz';");
tbladressen.first();
Vorname.setText(tbladressen.getString(3));
tbladressen.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Vorname;
}

private JTextField getNachname() throws SQLException {
if(Nachname == null) {
Nachname = new JTextField();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:eek:dbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Testdaten.mdb");
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet tbladressen = stmt2.executeQuery("SELECT * FROM [Adressen] WHERE [Vorname]='Vinzenz';");
tbladressen.first();
Nachname.setText(tbladressen.getString(4));
tbladressen.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Nachname;
}

private JTable getAccesstab() throws SQLException {
if(Accesstab == null) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:eek:dbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Testdaten.mdb");
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet tbladressen = stmt2.executeQuery("SELECT * FROM [Adressen];");
ResultSetMetaData rsmd = tbladressen.getMetaData();
int clmCnt = rsmd.getColumnCount();
String items1[] = new String[clmCnt];
tbladressen.first();
while (tbladressen.next()) {

for (int i = 1; i <= clmCnt-1; i++) {
items1=tbladressen.getString(i);
System.out.println(items1);
}
tbladressen.next();

TableModel AccesstabModel =
new DefaultTableModel(


// I think that's static
// new String[][] { { "Peter", "Fox" },
// { "John", "Miller" },
// { "Herbert", "House" } },
// new String[] { "Vorname", "Nachname" });


// Here I think should be placed the data from the database
new String[][] { { tbladressen.getString(3), tbladressen.getString(4) },

{ "Three", "Four" },
{ "Three", "Four" } },
new String[] { "Vorname", "Nachname" });
Accesstab = new JTable();
GroupLayout AccesstabLayout = new GroupLayout((JComponent)Accesstab);
Accesstab.setLayout(AccesstabLayout);
AccesstabLayout.setHorizontalGroup(AccesstabLayout.createParallelGroup());
AccesstabLayout.setVerticalGroup(AccesstabLayout.createParallelGroup());
Accesstab.setModel(AccesstabModel);

tbladressen.close();
con.close();



} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
return Accesstab;
}

private JScrollPane getJScrollPane1() throws SQLException {
if(jScrollPane1 == null) {
jScrollPane1 = new JScrollPane();
jScrollPane1.setPreferredSize(new java.awt.Dimension(3, 3));
jScrollPane1.setViewportView(getAccesstab());
}
return jScrollPane1;
}

}

Thank you
 
J

John B. Matthews

"Maenne said:
John B. Matthews said:
Maenne said:
I'm working with eclipse 3.4.1, java 2 jdk 1.4.2.01 and jigloo for
GUI.

Maybe someone can help me. First I'd build a GUI with jigloo.
(With a JTABLE). Then I created a JDBC-ODBC connection to a
MSAccess database. So far, so good.

Now my problem: I have no idea, how to connect the JTABLE with the
data from the database. I know, how to create the first column,
but how to place the WHILE-statement, so that i get all the data?
[...]

I don't know jigloo, MSAccess or ODBC, but you might want to look
at a simpler example to get started, perhaps something in a
tutorial:

<http://java.sun.com/docs/books/tutorial/jdbc/index.html>

Your SDK may include developer examples about using JTable with
JDBC; on my platform, it's found in Examples/Java/JFC/TableExample.
Also, you might look in the corresponding tutorial:

<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>
my problem is not to fill the JTABLE generally, that's not a problem.
My problem is that I fetched the data from the database.

Oh, that makes things easier. The TableExample I mentioned includes
code to adapt the JDBC interface to the TableModel interface
(JDBCAdapter). It's a little dated but well worth studying. It may not
be in current SDKs, but it's easy to find:

<http://www.google.com/search?q=TableExample+JDBCAdapter+TableSorter>

I would also reiterate RGB's helpful suggestions. In addition, please
trim signature lines when responding, and don't top-post.
 
R

RedGrittyBrick

Maenne wrote:

Please don't top-post, when replying, please put your new material at
the bottom or interleave it with the quoted material. Trim any quoted
material that you aren't replying to or which doesn't help other readers
understand.

Hi RedGrittyBrick,

at first, thank you for your answer. You're right, when you say, that MS Access is a poor Database, but in that case
it's the easiest way for me to get a connection to a database, by the way to learn java. Later, I try to use other db's.
No I got no SQL-Execptions. The fetching of the database data was successfully.

Here the complete coding without tabs:

Thanks.

Some general comments: You might like to read about the
Model-View-Controller (MVC) pattern. It can simplify things a lot if you
separate DBaccess into "Model" classes and keep your "View" classes
purely about user interaction.

You seem to be allowing SQLExceptions to be passed up the call-stack to
a point where they are handled by a general catch(Exception e) clause -
this seems a bit too general to me. I tend to log exceptions at the
earliest point and then use exception chaining to throw a more
application-specific exception.
package gui;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import javax.swing.SwingUtilities;
//Datenbank
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
private JMenuBar jMenuBar1;
private JMenu jMenu1;
private JMenu helpMenu;
private JMenuItem jMenuItem1;
private JScrollPane jScrollPane1;
private JTable Accesstab;
private JTextField Vorname;
private JTextField Nachname;
private static JTextField Eingabe;
private AbstractAction closeAboutAction;
private JButton jButton1;
private JLabel jLabel1;
private JDialog jDialog1;
private AbstractAction aboutAction;



/**
* Auto-generated main method to display this JFrame
* @throws Exception
*/
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);

}
});

}

public NewJFrame() {
super();

initGUI();

}




private void initGUI() {
try {

// dbload1();



GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
getContentPane().setLayout(thisLayout);
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
.addContainerGap(12, 12)
.add(getJScrollPane1(), GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
.add(59)
.add(getVorname(), GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.UNRELATED)
.add(getNachname(), GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addContainerGap(28, 28));
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(80, 80)
.add(getJScrollPane1(), GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.add(67)
.add(thisLayout.createParallelGroup()
.add(GroupLayout.BASELINE, getNachname(), GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.add(GroupLayout.BASELINE, getVorname(), GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
.addContainerGap(12, 12));
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);



this.setTitle("Hauptmenü");
{
jMenuBar1 = new JMenuBar();
setJMenuBar(jMenuBar1);
{
jMenu1 = new JMenu();
jMenuBar1.add(jMenu1);
jMenu1.setText("File");
}
{
helpMenu = new JMenu();
jMenuBar1.add(helpMenu);
helpMenu.setText("Help");
{
jMenuItem1 = new JMenuItem();
helpMenu.add(jMenuItem1);
jMenuItem1.setText("jMenuItem1");
jMenuItem1.setAction(getAboutAction());
}
}
}
pack();
this.setSize(400, 321);
} catch (Exception e) {
e.printStackTrace();
}
}

private AbstractAction getAboutAction() {
if(aboutAction == null) {
aboutAction = new AbstractAction("About", null) {
public void actionPerformed(ActionEvent evt) {
getJDialog1().pack();
getJDialog1().setLocationRelativeTo(null);
getJDialog1().setVisible(true);
try {
dbload1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
return aboutAction;
}

private JDialog getJDialog1() {
if(jDialog1 == null) {
jDialog1 = new JDialog(this);
GroupLayout jDialog1Layout = new GroupLayout((JComponent)jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1.setTitle("About");
{
jLabel1 = new JLabel();
jLabel1.setText("Hallo Sie");
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setFont(new java.awt.Font("Tahoma",1,16));
}
{
jButton1 = new JButton();
jButton1.setText("OK");
jButton1.setAction(getCloseAboutAction());
}
jDialog1Layout.setVerticalGroup(jDialog1Layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(getEingabe(), GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.add(148, 148, GroupLayout.PREFERRED_SIZE)
.add(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap());
jDialog1Layout.setHorizontalGroup(jDialog1Layout.createSequentialGroup()
.addContainerGap()
.add(jDialog1Layout.createParallelGroup()
.add(GroupLayout.LEADING, jDialog1Layout.createSequentialGroup()
.add(jLabel1, GroupLayout.PREFERRED_SIZE, 209, GroupLayout.PREFERRED_SIZE)
.add(0, 104, Short.MAX_VALUE)
.add(jButton1, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
.add(jDialog1Layout.createSequentialGroup()
.add(131, 131, GroupLayout.PREFERRED_SIZE)
.add(getEingabe(), GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
.add(17)))
.addContainerGap());
}
return jDialog1;
}

private AbstractAction getCloseAboutAction() {
if(closeAboutAction == null) {
closeAboutAction = new AbstractAction("OK", null) {
public void actionPerformed(ActionEvent evt) {
getJDialog1().dispose();
}
};
}
return closeAboutAction;
}
public static void dbload1() throws Exception {

}

private JTextField getEingabe() {
if(Eingabe == null) {
Eingabe = new JTextField();



}


return Eingabe;
}

private JTextField getVorname() throws SQLException {
if(Vorname == null) {
Vorname = new JTextField();
//Vorname.setText("Vorname");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:eek:dbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Testdaten.mdb");
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet tbladressen = stmt2.executeQuery("SELECT * FROM [Adressen] WHERE [Vorname]='Vinzenz';");
tbladressen.first();
Vorname.setText(tbladressen.getString(3));
tbladressen.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Vorname;
}

private JTextField getNachname() throws SQLException {
if(Nachname == null) {
Nachname = new JTextField();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:eek:dbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Testdaten.mdb");
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet tbladressen = stmt2.executeQuery("SELECT * FROM [Adressen] WHERE [Vorname]='Vinzenz';");
tbladressen.first();
Nachname.setText(tbladressen.getString(4));
tbladressen.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Nachname;
}

private JTable getAccesstab() throws SQLException {
if(Accesstab == null) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:eek:dbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Testdaten.mdb");
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet tbladressen = stmt2.executeQuery("SELECT * FROM [Adressen];");
ResultSetMetaData rsmd = tbladressen.getMetaData();
int clmCnt = rsmd.getColumnCount();
String items1[] = new String[clmCnt];
tbladressen.first();


while (tbladressen.next()) {

Here you correctly call .next() on the resultset.

for (int i = 1; i <= clmCnt-1; i++) {
items1=tbladressen.getString(i);
System.out.println(items1);
}


Here you extract the columns from the resultset.


tbladressen.next();

Here you call .next() a second time, throwing away the last row of table
data.



TableModel AccesstabModel =
new DefaultTableModel(

Here you construct a new DefaultTableModel for each row of the table.
That must wrong. You only want one model for the whole table.

// I think that's static
// new String[][] { { "Peter", "Fox" },
// { "John", "Miller" },
// { "Herbert", "House" } },
// new String[] { "Vorname", "Nachname" });


// Here I think should be placed the data from the database
new String[][] { { tbladressen.getString(3), tbladressen.getString(4) },

Here you extract column data a second time - why aren't you using items1[]

{ "Three", "Four" },
{ "Three", "Four" } },
new String[] { "Vorname", "Nachname" });
Accesstab = new JTable();
GroupLayout AccesstabLayout = new GroupLayout((JComponent)Accesstab);
Accesstab.setLayout(AccesstabLayout);
AccesstabLayout.setHorizontalGroup(AccesstabLayout.createParallelGroup());
AccesstabLayout.setVerticalGroup(AccesstabLayout.createParallelGroup());
Accesstab.setModel(AccesstabModel);

tbladressen.close();
con.close();



} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
return Accesstab;
}

private JScrollPane getJScrollPane1() throws SQLException {
if(jScrollPane1 == null) {
jScrollPane1 = new JScrollPane();
jScrollPane1.setPreferredSize(new java.awt.Dimension(3, 3));
jScrollPane1.setViewportView(getAccesstab());
}
return jScrollPane1;
}

}

Thank you

I would
1. Give Items1 a better name.
2. Give it an extra dimension so it can store all rows not just one.
3. Use a List rather than an array so I don't have to know
how many rows are needed.
4. Populate this using a while loop *before* creating
the DefaultTableModel
5. After collecting all the data, *then* create the DefaultTableModel
6. Pass all the data to the model in one go.

I always extend AbstractTableModel and populate it from an init() method
in that class - so there may be ways of using DefaultTableModel I am
unaware of and which are a better fit to your pattern of usage.

Do have a think about MVC
http://en.wikipedia.org/wiki/Model-view-controller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top