Hello,
I am trying to insert values from a .csv file into a MySQL database from a GUI. I cannot seem to accomplish this. I keep getting a null pointer exception where I set the connection properties in my main method. I also cannot seem to figure out how to use the importData() method after setting my variables in my GUI. I currently have that method called in the main method. I would appreciate any help anyone can give me. Thank you. Here is my code:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.sql.*;
import java.lang.Object;
public class ImportDataGUI extends javax.swing.JFrame {
String nameOfFile;
String tableName;
static ImportDataGUI db;
//Connection conn = db.connect("jdbc:mysql://localhost:3306/Registrar","root","Alyssa01");
/** Creates new form ImportDataGUI */
public ImportDataGUI() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tableLabel = new javax.swing.JLabel();
tableTextField = new javax.swing.JTextField();
fileLabel = new javax.swing.JLabel();
fileTextField = new javax.swing.JTextField();
fileSubmitButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tableLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
tableLabel.setText("Enter the table name below:");
fileLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
fileLabel.setText("Enter the file name to import below:");
fileSubmitButton.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
fileSubmitButton.setText("Submit");
fileSubmitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileSubmitButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(fileSubmitButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tableTextField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(fileLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tableLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fileTextField, javax.swing.GroupLayout.Alignment.LEADING))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(tableLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tableTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(fileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(9, 9, 9)
.addComponent(fileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(fileSubmitButton)
.addContainerGap(23, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void fileSubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {
nameOfFile = fileTextField.getText();
tableName = tableTextField.getText();
}
/**
* @param args the command line arguments
*/
public void importData(Connection conn,String filename) {
Statement stmt;
filename = nameOfFile;
try
{
String query = "";
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
if (tableName.equals("student")){
query = "LOAD DATA INFILE '+filename+' INTO TABLE student FIELDS TERMINATED BY ',' (StudentID,FirstName,LastName,DegreeProgram, Gender)";
}//end if
else if(tableName.equals("enrollment")){
query = "LOAD DATA INFILE '+filename+' INTO TABLE enrollment FIELDS TERMINATED BY ',' (StudentID, CourseID, Semester)";
}//end else if
else if(tableName.equals("course")){
query = "LOAD DATA INFILE '+filename+' INTO TABLE course FIELDS TERMINATED BY ',' (courseID,courseName,section,Instructor)";
}//end else if
else {
JOptionPane.showMessageDialog(null,"Invalid entry.");
}
stmt.executeUpdate(query);
}
catch(Exception e)
{
e.printStackTrace();
stmt = null;
}
}//end importData
public Connection connect(String db_connect_str,String db_userid, String db_password){
Connection conn;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(db_connect_str, db_userid, db_password);
}
catch(Exception e)
{
e.printStackTrace();
conn = null;
}
return conn;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ImportDataGUI().setVisible(true);
}
});
Connection conn = db.connect("jdbc:mysql://localhost:3306/Registrar","root","Alyssa01");
db.importData(conn,args[0]);
}
// Variables declaration - do not modify
private javax.swing.JLabel fileLabel;
private javax.swing.JButton fileSubmitButton;
private javax.swing.JTextField fileTextField;
private javax.swing.JLabel tableLabel;
private javax.swing.JTextField tableTextField;
// End of variables declaration
}//end ImportDataGUI