dcsimg
Adding data to database via JList
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jas_singh
Posted On:   Wednesday, March 7, 2007 04:03 PM

Hi, ive got a problem adding data to the database from JList. The function allows me to remove data from the database although when i attempt to add data to the database i seem to come across errors, the program seems to add the data to the list and not to the database. I tried the sql statement on another application and it worked fine. The error that occurs is the following: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (what does this error mean?) import javax.swing.BorderFactory; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPan   More>>

Hi, ive got a problem adding data to the database from JList. The function allows me to remove data from the database although when i attempt to add data to the database i seem to come across errors, the program seems to add the data to the list and not to the database. I tried the sql statement on another application and it worked fine. The error that occurs is the following: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (what does this error mean?)

			

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.*;
//public Connection conn;
//public Statement stat;

public class ListFrame extends JFrame
{
private JPanel backPanel;
private JPanel inputPanel;
private JTextField inputField;
private JPanel buttonPanel;
private JButton removeButton;
private JButton addButton;
private JScrollPane scroller;
private JPanel database;
private JList list;
private DefaultListModel model, model2;
private Connection conn;
private Statement stat;
StringBuffer results = new StringBuffer();
Vector vector1 = new Vector();
String s;
public ListFrame()
{
super("List Frame");
createGUI();
setModel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(400, 400);
setVisible(true);
}

private void setModel()
{
//model = new DefaultListModel();
// connecttodb();
model = new DefaultListModel();
connecttodb();
list.setModel(model);
//connecttodb();
//model.addElement(vector1);
//connecttodb(database);
}

private void createGUI()
{
//connecttodb();

createScrollerPanel();
createButtonPanel();
createInputPanel();
// connecttodb();
createBackPanel();

add(backPanel, BorderLayout.CENTER);
}

private void createScrollerPanel()
{
list = new JList(vector1);
list.addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
listClicked();
}
});

scroller = new JScrollPane(list);

}

private void connecttodb(){

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///wordlist", "Username", "Password");
Statement stat = conn.createStatement();

ResultSet rs = stat.executeQuery("SELECT * FROM wordlist");
//list.setModel(model);
Vector vector1 = new Vector();
while(rs.next()) vector1.add(rs.getObject(1));
//String s = vector1.toString();
//model.addElement(iter.next());

//Looping through vector and adding each individual element to model
ListIterator iter = vector1.listIterator();
while (iter.hasNext()) {
//System.out.println((String)iter.next());
model.addElement(iter.next());
//vector1.add(s);
//model.addElement(vector1.elements());
//vector1.get(i)
}


//list = new JList(model);
//list.setVisibleRowCount( 9 );
list.repaint();

}
catch(SQLException sqlException)
{
JOptionPane.showMessageDialog(null,sqlException.getMessage(),
"Database Error1", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}

catch (ClassNotFoundException classNotFound)
{
JOptionPane.showMessageDialog(null, classNotFound.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE);
System.exit(1);

}




}



private void createButtonPanel()
{
addButton = new JButton("Add");
addButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addButtonClicked();
}
});
removeButton = new JButton("Remove");
// removeButton.setEnabled(false);
removeButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
removeButtonClicked();
}
});
buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
}

private void createInputPanel()
{
inputPanel = new JPanel(new BorderLayout());
inputPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
inputField = new JTextField();
inputPanel.add(inputField, BorderLayout.NORTH);
inputPanel.add(buttonPanel, BorderLayout.CENTER);
}

private void createBackPanel()
{
// connecttodb();
backPanel = new JPanel(new BorderLayout());
backPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
backPanel.add(scroller, BorderLayout.CENTER);
backPanel.add(inputPanel, BorderLayout.SOUTH);

}

private void addToDatabase(){

try {
Statement statement = conn.createStatement();

if ( !inputField.getText().equals( "" ) ) {
String query = "INSERT INTO wordlist (" +
"Words" +
") VALUES ('" +


inputField.getText() + "')";
conn.nativeSQL( query );
statement.executeUpdate( query );

}


}

catch ( SQLException sqlex ) {
// append( sqlex.toString() );

}
// inputField.setText("");
//list.repaint();
//list = new JList(vector1);
// connecttodb();

}


private void deleteFromDatabase(){


try {

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///wordlist", "Username", "Password");

Statement statement = conn.createStatement();

String word = inputField.getText();
// DO SOME SANITY CHECKS ON word HERE!!!
String query = "DELETE FROM wordlist WHERE Words='" + word + "'";
statement.executeUpdate(query);


// conn.nativeSQL( query );
statement.executeUpdate(query);

}//End try
catch ( SQLException sqlex ) {
System.err.println( sqlex.toString() );

return;
}//End catch
catch ( Exception excp ) {
// process remaining Exceptions here
System.err.println( excp.toString(
) );

return;
}//End try-catch block

// }//End for loop


}




private void addButtonClicked()
{
// model.clear();
s = inputField.getText();
if (s.length() > 0)
{
model.addElement(s);
// s= vector1.toString();
// model.addElement(s);
// list = new JList(vector1);
// model.clear();
// connecttodb();
//addToDatabase();
}
inputField.setText("");
addToDatabase();


}

private void removeButtonClicked()
{
int n = list.getSelectedIndex();
if (!(n < 0) || (n > model.size()))
{
model.remove(n);
}
// removeButton.setEnabled(false);
deleteFromDatabase();
}

private void listClicked()
{
int n = list.getSelectedIndex();
if (!(n < 0) || (n > model.size()))
{
removeButton.setEnabled(true);
}

}

public static void main(String[] args)
{

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ListFrame();

}
});
}
}


Thanks in advance.
Regards, Jas
Jas    <<Less
About | Sitemap | Contact