Advertisement
Guest User

Database RPG-kun

a guest
Sep 16th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.18 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package gui.project;
  7.  
  8. import java.sql.*;
  9. import java.util.ArrayList;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.table.DefaultTableModel;
  12. import javax.swing.table.TableModel;
  13.  
  14. public class frmStudent extends javax.swing.JFrame {
  15.  
  16. /**
  17. * Creates new form frmStudent
  18. */
  19. public frmStudent() {
  20. initComponents();
  21. Show_Users_In_JTable();
  22. //C:\Program Files\NetBeans 8.1\ide\modules\ext\mysql-connector-java-5.1.23-bin.jar
  23. }
  24. public Connection getConnection(){
  25. Connection con;
  26. try {
  27. con = DriverManager.getConnection("jdbc:mysql://localhost/dbstudentinfo", "root","");
  28. return con;
  29.  
  30. }catch(Exception e){
  31. e.printStackTrace();
  32. return null;
  33. }
  34.  
  35. }
  36. public ArrayList<Source> getUsersList()
  37. {
  38. ArrayList<Source> usersList = new ArrayList<Source>();
  39. Connection connection = getConnection();
  40.  
  41. String query = "SELECT * FROM tblstudentdata";
  42. Statement st;
  43. ResultSet rs;
  44.  
  45. try {
  46. st = connection.createStatement();
  47. rs = st.executeQuery(query);
  48. Source source;
  49. while(rs.next())
  50. {
  51. source = new Source(rs.getInt("flidid"),rs.getString("fldfirstname"),rs.getString("fldlastname"),rs.getString("fldmiddlename"),rs.getString("fldcourse"));
  52. usersList.add(source);
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. return usersList;
  58. }
  59. public void Show_Users_In_JTable()
  60. {
  61. ArrayList<Source> list = getUsersList();
  62. DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
  63. Object[] row = new Object[5];
  64. for(int i = 0; i < list.size(); i++)
  65. {
  66. row[0] = list.get(i).getID();
  67. row[1] = list.get(i).getfirstname();
  68. row[2] = list.get(i).getlastname();
  69. row[3] = list.get(i).getmiddlename();
  70. row[4] = list.get(i).getcourse();
  71. model.addRow(row);
  72. }
  73. }
  74.  
  75.  
  76. /**
  77. * This method is called from within the constructor to initialize the form.
  78. * WARNING: Do NOT modify this code. The content of this method is always
  79. * regenerated by the Form Editor.
  80. */
  81. @SuppressWarnings("unchecked")
  82. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  83. private void initComponents() {
  84.  
  85. jLabel1 = new javax.swing.JLabel();
  86. jLabel2 = new javax.swing.JLabel();
  87. jLabel3 = new javax.swing.JLabel();
  88. jLabel4 = new javax.swing.JLabel();
  89. jLabel5 = new javax.swing.JLabel();
  90. txtSid = new javax.swing.JTextField();
  91. txtFname = new javax.swing.JTextField();
  92. txtMname = new javax.swing.JTextField();
  93. txtLname = new javax.swing.JTextField();
  94. txtCourse = new javax.swing.JTextField();
  95. jScrollPane1 = new javax.swing.JScrollPane();
  96. jTable1 = new javax.swing.JTable();
  97. cmdAdd = new javax.swing.JButton();
  98. cmdDelete = new javax.swing.JButton();
  99. cmdUpdate = new javax.swing.JButton();
  100.  
  101. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  102.  
  103. jLabel1.setText("Student ID");
  104.  
  105. jLabel2.setText("First Name");
  106.  
  107. jLabel3.setText("Middle Name");
  108.  
  109. jLabel4.setText("Last Name");
  110.  
  111. jLabel5.setText("Course");
  112.  
  113. txtSid.addActionListener(new java.awt.event.ActionListener() {
  114. public void actionPerformed(java.awt.event.ActionEvent evt) {
  115. txtSidActionPerformed(evt);
  116. }
  117. });
  118.  
  119. txtMname.addActionListener(new java.awt.event.ActionListener() {
  120. public void actionPerformed(java.awt.event.ActionEvent evt) {
  121. txtMnameActionPerformed(evt);
  122. }
  123. });
  124.  
  125. txtLname.addActionListener(new java.awt.event.ActionListener() {
  126. public void actionPerformed(java.awt.event.ActionEvent evt) {
  127. txtLnameActionPerformed(evt);
  128. }
  129. });
  130.  
  131. jTable1.setModel(new javax.swing.table.DefaultTableModel(
  132. new Object [][] {
  133.  
  134. },
  135. new String [] {
  136. "Student ID", "First Name", "Last Name", "Middle Name", "Course"
  137. }
  138. ) {
  139. boolean[] canEdit = new boolean [] {
  140. false, false, false, false, false
  141. };
  142.  
  143. public boolean isCellEditable(int rowIndex, int columnIndex) {
  144. return canEdit [columnIndex];
  145. }
  146. });
  147. jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
  148. public void mouseClicked(java.awt.event.MouseEvent evt) {
  149. jTable1MouseClicked(evt);
  150. }
  151. });
  152. jScrollPane1.setViewportView(jTable1);
  153. if (jTable1.getColumnModel().getColumnCount() > 0) {
  154. jTable1.getColumnModel().getColumn(0).setResizable(false);
  155. jTable1.getColumnModel().getColumn(1).setResizable(false);
  156. jTable1.getColumnModel().getColumn(2).setResizable(false);
  157. jTable1.getColumnModel().getColumn(3).setResizable(false);
  158. jTable1.getColumnModel().getColumn(4).setResizable(false);
  159. }
  160.  
  161. cmdAdd.setText("Add");
  162. cmdAdd.addActionListener(new java.awt.event.ActionListener() {
  163. public void actionPerformed(java.awt.event.ActionEvent evt) {
  164. cmdAddActionPerformed(evt);
  165. }
  166. });
  167.  
  168. cmdDelete.setText("Delete");
  169. cmdDelete.addActionListener(new java.awt.event.ActionListener() {
  170. public void actionPerformed(java.awt.event.ActionEvent evt) {
  171. cmdDeleteActionPerformed(evt);
  172. }
  173. });
  174.  
  175. cmdUpdate.setText("Update");
  176. cmdUpdate.addActionListener(new java.awt.event.ActionListener() {
  177. public void actionPerformed(java.awt.event.ActionEvent evt) {
  178. cmdUpdateActionPerformed(evt);
  179. }
  180. });
  181.  
  182. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  183. getContentPane().setLayout(layout);
  184. layout.setHorizontalGroup(
  185. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  186. .addGroup(layout.createSequentialGroup()
  187. .addContainerGap()
  188. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  189. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
  190. .addGroup(layout.createSequentialGroup()
  191. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  192. .addGroup(layout.createSequentialGroup()
  193. .addComponent(jLabel4)
  194. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
  195. .addComponent(txtLname, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
  196. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  197. .addComponent(jLabel3)
  198. .addGap(18, 18, 18)
  199. .addComponent(txtMname))
  200. .addGroup(layout.createSequentialGroup()
  201. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  202. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  203. .addComponent(jLabel2))
  204. .addGap(14, 14, 14)
  205. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  206. .addComponent(txtSid)
  207. .addComponent(txtFname, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))))
  208. .addGap(18, 18, 18)
  209. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  210. .addGroup(layout.createSequentialGroup()
  211. .addComponent(jLabel5)
  212. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  213. .addComponent(txtCourse, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
  214. .addGroup(layout.createSequentialGroup()
  215. .addComponent(cmdAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
  216. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  217. .addComponent(cmdDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
  218. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  219. .addComponent(cmdUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
  220. .addGap(0, 0, Short.MAX_VALUE)))
  221. .addContainerGap())
  222. );
  223. layout.setVerticalGroup(
  224. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  225. .addGroup(layout.createSequentialGroup()
  226. .addContainerGap()
  227. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  228. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
  229. .addComponent(txtSid)
  230. .addComponent(jLabel5)
  231. .addComponent(txtCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  232. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  233. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  234. .addComponent(jLabel2)
  235. .addComponent(txtFname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  236. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  237. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  238. .addComponent(jLabel3)
  239. .addComponent(txtMname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  240. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  241. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  242. .addGroup(layout.createSequentialGroup()
  243. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  244. .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
  245. .addComponent(txtLname, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  246. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  247. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))
  248. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  249. .addComponent(cmdAdd)
  250. .addComponent(cmdDelete)
  251. .addComponent(cmdUpdate)))
  252. .addGap(162, 162, 162))
  253. );
  254.  
  255. pack();
  256. }// </editor-fold>
  257.  
  258. private void txtSidActionPerformed(java.awt.event.ActionEvent evt) {
  259. // TODO add your handling code here:
  260. }
  261.  
  262. private void txtMnameActionPerformed(java.awt.event.ActionEvent evt) {
  263. // TODO add your handling code here:
  264. }
  265.  
  266. private void txtLnameActionPerformed(java.awt.event.ActionEvent evt) {
  267. // TODO add your handling code here:
  268. }
  269.  
  270. private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
  271. // Get The Index Of The Slected Row
  272. int i = jTable1.getSelectedRow();
  273. TableModel model = jTable1.getModel();
  274. // Display Slected Row In JTexteFields
  275. txtSid.setText(model.getValueAt(i,0).toString());
  276. txtFname.setText(model.getValueAt(i,1).toString());
  277. txtLname.setText(model.getValueAt(i,2).toString());
  278. txtMname.setText(model.getValueAt(i,3).toString());
  279. txtCourse.setText(model.getValueAt(i,4).toString());
  280.  
  281. }
  282.  
  283. private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {
  284. String query = "INSERT INTO tblstudentdata(flidid, fldfirstname, fldlastname,fldcourse,fldmiddlename) VALUES ('"
  285. +Integer.parseInt(txtSid.getText())+"','"+txtFname.getText()+"','"+txtLname.getText()
  286. +"','"+txtCourse.getText()+"','"+txtMname.getText()+"')";
  287. executeSQlQuery(query, "Inserted"); // TODO add your handling code here:
  288. }
  289.  
  290. private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {
  291. String query = "UPDATE tblstudentdata SET flidid='"+Integer.parseInt(txtSid.getText())
  292. +"',fldfirstname='"+txtFname.getText()+"',fldlastname='"+txtLname.getText()
  293. +"',fldcourse='"+txtCourse.getText()+"',fldmiddlename='"+txtMname.getText()
  294. +"' WHERE flidid = ('"+Integer.parseInt(txtSid.getText())+"')";
  295. executeSQlQuery(query, "Updated");
  296. }
  297.  
  298. private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {
  299. String query = "DELETE FROM tblstudentdata WHERE flidid = ('"+Integer.parseInt(txtSid.getText())+"')";
  300. executeSQlQuery(query, "Deleted");
  301. }
  302. public void executeSQlQuery(String query, String message)
  303. {
  304. Connection con = getConnection();
  305. Statement st;
  306. try{
  307. st = con.createStatement();
  308. if((st.executeUpdate(query)) == 1)
  309. {
  310. // refresh jtable data
  311. DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
  312. model.setRowCount(0);
  313. Show_Users_In_JTable();
  314.  
  315. JOptionPane.showMessageDialog(null, "Data "+message+" Succefully");
  316. }else{
  317. JOptionPane.showMessageDialog(null, "Data Not "+message);
  318. }
  319. }catch(Exception ex){
  320. ex.printStackTrace();
  321. }
  322. }
  323.  
  324. /**
  325. * @param args the command line arguments
  326. */
  327. public static void main(String args[]) {
  328. /* Set the Nimbus look and feel */
  329. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  330. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  331. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  332. */
  333. try {
  334. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  335. if ("Nimbus".equals(info.getName())) {
  336. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  337. break;
  338. }
  339. }
  340. } catch (ClassNotFoundException ex) {
  341. java.util.logging.Logger.getLogger(frmStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  342. } catch (InstantiationException ex) {
  343. java.util.logging.Logger.getLogger(frmStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  344. } catch (IllegalAccessException ex) {
  345. java.util.logging.Logger.getLogger(frmStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  346. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  347. java.util.logging.Logger.getLogger(frmStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  348. }
  349. //</editor-fold>
  350.  
  351. /* Create and display the form */
  352. java.awt.EventQueue.invokeLater(new Runnable() {
  353. public void run() {
  354. new frmStudent().setVisible(true);
  355. }
  356. });
  357. }
  358.  
  359. // Variables declaration - do not modify
  360. private javax.swing.JButton cmdAdd;
  361. private javax.swing.JButton cmdDelete;
  362. private javax.swing.JButton cmdUpdate;
  363. private javax.swing.JLabel jLabel1;
  364. private javax.swing.JLabel jLabel2;
  365. private javax.swing.JLabel jLabel3;
  366. private javax.swing.JLabel jLabel4;
  367. private javax.swing.JLabel jLabel5;
  368. private javax.swing.JScrollPane jScrollPane1;
  369. private javax.swing.JTable jTable1;
  370. private javax.swing.JTextField txtCourse;
  371. private javax.swing.JTextField txtFname;
  372. private javax.swing.JTextField txtLname;
  373. private javax.swing.JTextField txtMname;
  374. private javax.swing.JTextField txtSid;
  375. // End of variables declaration
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement