So I wrote this code for a POS system that would prompt a window where you can see all the employee information. You simply press fetch data on the JFrame app and it is suppose to generate a table that was made on SQLITE. Unfortunately, I can't get it to connect and was wondering where I am going wrong. Any input would be helpful.
Thanks.
import java.sql.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class connection extends javax.swing.JFrame
{
Connection conn=null;
ResultSet rs = null;
PreparedStatement pst=null;/**
* Creates new form connection
*/
public connection()
{
initComponents();
conn=javaconnect.ConnecrDb();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{
jScrollPane1 = new javax.swing.JScrollPane();
employeeinfo = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
employeeinfo.setModel(new javax.swing.table.DefaultTableModel(
new Object [][]
{
},
new String []
{
"employeeid", "firstname", "lastname", "username", "password", "clockin", "clockout"
}
));
jScrollPane1.setViewportView(employeeinfo);
jButton1.setText("Fetch Data");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton1ActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(160, 160, 160)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(42, 42, 42)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(40, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String sql = "select * from employeeinfo";
DefaultTableModel model=(DefaultTableModel)employeeinfo.getModel();
try
{
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Andres\\Documents\\NetBeansProjects\\POS\\employeeinfo.sqlite");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
String d1=rs.getString("employeeid");
String d2=rs.getString("firstname");
String d3=rs.getString("lastname");
String d4=rs.getString("username");
String d5=rs.getString("password");
String d6=rs.getString("clockin");
String d7=rs.getString("clockout");
model.addRow(new Object[]{d1,d2,d3,d4,d5,d6,d7});
}
JOptionPane.showMessageDialog(null, "Connection Established");
rs.close();
st.close();
conn.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Error in Connection");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://ift.tt/1cNmMj1
*/
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new connection().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTable employeeinfo;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
Aucun commentaire:
Enregistrer un commentaire