mardi 13 janvier 2015

Method Not Producing JTable

I work for my local probate court, and one service we offer is email reminders. They fill out a form, it's entered in the system, and they will be notified when something is due. I've been allowed to create a program that will allow me to enter the information from the form to keep track of the attorney's who have signed up.



import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Attempt1 {

public static void setConnection() throws SQLException {
Connection conn = null;
Statement state = null;

try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:dummy3.db");
} catch (Exception e) {
e.printStackTrace();
}

setStatement(conn, state);
setInsert(conn, state);
setQuery(conn, state);

}

// Test method
public static void setStatement(Connection conn, Statement state)
throws SQLException {

state = conn.createStatement();

String sql = "CREATE TABLE IF NOT EXISTS ATTORNEY "
+ "(ID INT PRIMARY KEY NOT NULL,"
+ " NAME TEXT NOT NULL, "
+ " EMAIL CHAR(50), " + " DATE TEXT NOT NULL)";
state.executeUpdate(sql);

}

public static void setInsert(Connection conn, Statement state)
throws SQLException {

int entries;
int i = 0;
String input;
String sql = "";

state = conn.createStatement();

Scanner keyboard = new Scanner(System.in);

System.out.print("How many entries will you insert?");
entries = keyboard.nextInt();
keyboard.nextLine();

while (i < entries) {

System.out
.println("Enter the values for ID, NAME, EMAIL, and DATE");
input = keyboard.nextLine();

sql = "INSERT INTO ATTORNEY (ID, NAME, EMAIL, DATE) " + "VALUES ("
+ input + ");";

state.executeUpdate(sql);

i++;
}

}

// Test method
public static void setQuery(Connection conn, Statement state)
throws SQLException {

state = conn.createStatement();

String sql = "SELECT * FROM ATTORNEY";



ResultSet result = state.executeQuery(sql);

/*
* while (result.next()) {
*
* String name = result.getString("NAME"); long age =
* result.getLong("ID");
*
* System.out.println(name); System.out.println(age); }
*/

JTable table = new JTable(buildTableModel(result));

JOptionPane.showMessageDialog(null, new JScrollPane(table));

result.close();
state.close();

}

public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {

ResultSetMetaData metaData = rs.getMetaData();

// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}

// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}

return new DefaultTableModel(data, columnNames);

}

public static void main(String[] args) throws SQLException {

setConnection();

}
}


The issue I have is the method setQuery. It displays a table with the data entered. Where it currently sits at line 21, it does not display the table, nor does it seem to terminate the program. The goal was to update the table with a few entries, and then display the Jtable. When placed above the setInsert method, it displays just fine, and will reflect the new entries added the next time you start it. I'm still learning Java outside of a classroom environment on my own, so I can only imagine that this is riddled with poor coding, but I just can't seem to figure out why it doesn't display the table after the setInsert method is called, although I assume it's something painfully obvious. In it's current state, this program is just meant to test functions of SQLite with Java. From there it will be created with a GUI.


Aucun commentaire:

Enregistrer un commentaire