Home arrow static arrow Java Programming [Archive] - Accessing a database
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Accessing a database
This topic has 5 replies on 1 page.

Posts:1
Registered: 8/5/04
Accessing a database  
Aug 5, 2004 8:24 AM



 
I�m trying to create a program to access a database using SQL and Java the other thing it�s I�m not sure if it�s the best way to do it please

/*
* SimpleTableDemo2 To access a database
*
*/

import java.util.*;
import java.sql.*;
import java.io.*;
import java.lang.Object;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimpleTableDemo2 extends JPanel {
private boolean DEBUG = false;

public SimpleTableDemo2() {
super(new GridLayout(1,0));

String[] columnNames = {"Part Number", "Type of Shippment"};
Object[][] data;
DatabaseExample2 BaseDatos = new DatabaseExample2();
BaseDatos.loadDriver();
BaseDatos.makeConnection();

try {

BaseDatos.stmt = BaseDatos.con.createStatement();
BaseDatos.result = BaseDatos.stmt.executeQuery("select fgpart, fgcnid from cmsdat.bold where fgbol# = 163340 ");

String partNumber;
String typeShippment;
int x = 0;
while (BaseDatos.result.next()) {

partNumber = BaseDatos.result.getString("fgpart");
typeShippment = BaseDatos.result.getString("fgcnid");
data[x][0] = partNumber;
data[x][1] = typeShippment;
x += 1;
}
}catch(SQLException ex) {
System.err.println("Can not connect to AS/400" + ex.getMessage());

}

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));

if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}

//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() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
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();
}
});
}
}


check the following error.

SimpleTableDemo2.java [44:1] variable data might not have been initialized
data[x][0] = partNumber;
^
SimpleTableDemo2.java [54:1] variable data might not have been initialized
final JTable table = new JTable(data, columnNames);
^
2 errors
Errors compiling SimpleTableDemo2.

 

Posts:6,750
Registered: 1/25/04
Re: Accessing a database  
Aug 5, 2004 8:30 AM (reply 1 of 5)



 
I�m trying to create a program to access a database
using SQL and Java the other thing it�s I�m not sure
if it�s the best way to do it please

Obviously not, since it doesn't compile. Please don't post huge quantities of irrelevant code. When you do post code, please use code tags (see the Formatting Help link).

SimpleTableDemo2.java [44:1] variable data might not
have been initialized
data[x][0] = partNumber;
^
SimpleTableDemo2.java [54:1] variable data might not
have been initialized
final JTable table = new JTable(data, columnNames);
^
2 errors
Errors compiling SimpleTableDemo2.

You must set local variables to something before using them. In this case, probably a new int[][] would be appropriate (or perhaps double[][] or whatever your data type is).
 

Posts:373
Registered: 5/8/01
Re: Accessing a database  
Aug 5, 2004 8:35 AM (reply 2 of 5)



 
Hi,
the compiler tells you that you try to do something w/ the variable data but you didn't initialise it properly. You define the variable with
Object[][] data;

but you didn't really create the array.
But your real problem is that you can't use a 2-dimensional array. You don't know how many rows ou will get, but you need this information when creating the array. It would be better to use ArrayList to hold you rows so you cn add an element for each row you get from you DB.

Andr�
 

Posts:6,750
Registered: 1/25/04
Re: Accessing a database  
Aug 5, 2004 8:42 AM (reply 3 of 5)



 
Lists are one-dimensional, the analog to an array of arrays is a List of Lists. I would recommend making a custom class that represents one row, and create one instance of it per row. Put those in a List.
 

Posts:373
Registered: 5/8/01
Re: Accessing a database  
Aug 5, 2004 10:18 AM (reply 4 of 5)



 
Of course they are 1-dimensional. I wrote to use an ArrayList for the rows. For the entries in each row you could still use an array.
 

Posts:957
Registered: 3/31/04
Re: Accessing a database  
Aug 5, 2004 10:26 AM (reply 5 of 5)



 
The lazy way:

Object[][] data = null;
 
This topic has 5 replies on 1 page.