I have created a JTable which consists of 2 columns. The first one is the Item and the second one is the Quantity.
In my db, i have created 3 columns namely Item,Quantity, and date. Date column inserts a date automatically when a new row is created.
Now, i get this error when i run my program:
C:\JAVA2>java GroceryList
Driver loaded
Potatoes
3
Databse Connected
rows:1columns:2
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Number of query
values and destination fields are not the same.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
at GroceryList$Listener.actionPerformed(GroceryList.java:108)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Here's my code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import javax.swing.table.*;
import java.sql.*;
public class GroceryList extends JFrame {
private JLabel jlblStatus = new JLabel();
// TABLE DESIGN
private String[] columnNames =
{"Item", "Quantity"};
// Create table data
private Object[][] rowData = {
{"Potatoes", 3 }
};
private DefaultTableModel tableModel = new DefaultTableModel(
rowData, columnNames);
// Create a table
private JTable jTable1 = new JTable(tableModel);
// create save button
JButton jbtSave = new JButton("Submit");
JButton jbtAddRow = new JButton("Add Row");
//create panel
JPanel p1 = new JPanel();
Connection conn;
Statement s;
public GroceryList()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
}
catch (Exception ex)
{
ex.printStackTrace();
}
p1.add(new JScrollPane(jTable1),
BorderLayout.CENTER);
p1.add(jbtSave,BorderLayout.CENTER);
p1.add(jbtAddRow,BorderLayout.CENTER);
add(p1,BorderLayout.WEST);
jbtSave.addActionListener(new Listener());
jbtAddRow.addActionListener(new Listener());
getTableData(jTable1);
}
class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbtSave)
{
try
{
// Establish a connection
conn = DriverManager.getConnection
("jdbc:odbc:JUMAWIND");
System.out.println("Databse Connected");
s = conn.createStatement();
String sql = "INSERT INTO GroceryList VALUES (?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
TableModel tm = jTable1.getModel();
int nRow = tm.getRowCount(), nCol = tm.getColumnCount();
System.out.println( " rows:" + nRow + "columns:" + nCol);
for(int r = 0;r < nRow; r++)
{
for(int c = 1;c <= nCol;c++)
{
if (c == 1 )
{
ps.setString(c, tm.getValueAt(r, c-1).toString());
}
else
{
ps.setInt(c, Integer.parseInt(tm.getValueAt(r, c-1).toString()));
}
}
ps.executeUpdate();
}
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
if(e.getSource() == jbtAddRow)
{
if (jTable1.getSelectedRow() >= 0)
tableModel.insertRow(jTable1.getSelectedRow(),
new java.util.Vector());
else
tableModel.addRow(new java.util.Vector());
}
}
}
// I have created this method just in cas eif i wanna use it .
private Vector getColumnNames()
{
Vector<String> columnNames = new Vector<String>();
for (int i = 0; i < jTable1.getColumnCount(); i++)
columnNames.add(jTable1.getColumnName(i));
return columnNames;
}
// This method accesses data from the JTable
public Object[][] getTableData (JTable table)
{
int nRow = tableModel.getRowCount(), nCol = tableModel.getColumnCount();
Object[][] tableData = new Object[nRow][nCol];
for (int i = 0 ; i < nRow ; i++)
for (int j = 0 ; j < nCol ; j++)
{
tableData[i][j] = tableModel.getValueAt(i,j);
System.out.println(tableData[i][j]);
}
return tableData;
}
// Main Method
public static void main(String[] args) {
JFrame frame = new GroceryList();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Grocery List");
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}
I will very much appreciate your thought and ideas.Fell free to share your views.

New Topic/Question
Reply


MultiQuote



|