You are on page 1of 6

guiBackup.

java
----------------------------------------------------------------------------------------------------------------------------------------------------------------------package SAD; import java.sql.*; import java.util.Date; import java.text.*; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JOptionPane; public class guiBackup extends javax.swing.JFrame { public static Connection conn; public guiBackup() { initComponents(); conn = guiLogin.conn; } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); generateInternalFrame = new javax.swing.JInternalFrame(); jScrollPane1 = new javax.swing.JScrollPane(); statusTextArea = new javax.swing.JTextArea(); generateButton = new javax.swing.JButton(); uploadButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Sheetal Corporation"); jPanel1.setBackground(new java.awt.Color(102, 204, 240)); generateInternalFrame.setTitle("Query"); generateInternalFrame.setVisible(true); statusTextArea.setColumns(20); statusTextArea.setRows(5); jScrollPane1.setViewportView(statusTextArea); javax.swing.GroupLayout generateInternalFrameLayout = new javax.swing.GroupLayout(generateInternalFrame.getContentPane()); generateInternalFrame.getContentPane().setLayout(generateInternalFrameLayout); generateInternalFrameLayout.setHorizontalGroup( generateInternalFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE) );

generateInternalFrameLayout.setVerticalGroup( generateInternalFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE) ); generateButton.setText("Generate"); generateButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateButtonActionPerformed(evt); } }); uploadButton.setText("Restore"); uploadButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { uploadButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(generateInternalFrame) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(139, 139, 139) .addComponent(generateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(101, 101, 101) .addComponent(uploadButton) .addContainerGap(205, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(generateInternalFrame, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(generateButton) .addComponent(uploadButton)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) );

layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-649)/2, (screenSize.height-416)/2, 649, 416); }// </editor-fold>//GEN-END:initComponents private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_generateButtonActionPerformed String SQL1 = "SELECT * FROM customer"; String SQL2 = "SELECT * FROM logins"; String SQL3 = "SELECT * FROM privilege"; String SQL4 = "SELECT * FROM product"; String SQL5 = "SELECT * FROM sales"; //customer String id = ""; String last = ""; String add = ""; String contact = ""; String first = ""; String ini = ""; //logins String lu = ""; String dt = ""; //privilege String un = ""; String lvl = ""; //products String pc = ""; String pn = ""; String pd = ""; String pp = ""; String pq = ""; //sales String oc =""; String ci = ""; String prc = ""; String ic = ""; String on = ""; String dte = ""; String tp = ""; String quantity = "";

try{ PreparedStatement stmt = conn.prepareStatement(SQL1); ResultSet rs = stmt.executeQuery(); PreparedStatement stmt2 = conn.prepareStatement(SQL2); ResultSet rs2 = stmt2.executeQuery(); PreparedStatement stmt3 = conn.prepareStatement(SQL3); ResultSet rs3 = stmt3.executeQuery(); PreparedStatement stmt4 = conn.prepareStatement(SQL4); ResultSet rs4 = stmt4.executeQuery(); PreparedStatement stmt5 = conn.prepareStatement(SQL5); ResultSet rs5 = stmt5.executeQuery(); statusTextArea.append("Create schema sad;\nCREATE DATABASE IF NOT EXISTS sad;\nUSE sad;\n\n"); statusTextArea.append("DROP TABLE IF EXISTS customer;\n"); statusTextArea.append("DROP TABLE IF EXISTS logins;\n"); statusTextArea.append("DROP TABLE IF EXISTS privilege;\n"); statusTextArea.append("DROP TABLE IF EXISTS product;\n"); statusTextArea.append("DROP TABLE IF EXISTS sales;\n"); statusTextArea.append("\n\n\n"); statusTextArea.append("CREATE TABLE customer(\ncustomer_id INTEGER(10) NOT NULL,\ncustomer_last VARCHAR(60) NOT NULL,\ncustomer_address VARCHAR(90) NOT NULL,\ncontact_number VARCHAR(15) NOT NULL,\ncustomer_first VARCHAR(45) NOT NULL,\ncustomer_ini VARCHAR(2) NOT NULL,\nPRIMARY KEY(customer_id)\n); "); statusTextArea.append("\n\n\n"); statusTextArea.append("CREATE TABLE logins(\nloguser VARCHAR(32) NOT NULL,\ndate VARCHAR(50)\n);"); statusTextArea.append("\n\n\n"); statusTextArea.append("CREATE TABLE privilege(\nusername VARCHAR(10) NOT NULL,\nlevel INTEGER(100) NOT NULL,\nPRIMARY KEY(username)\n);"); statusTextArea.append("\n\n\n"); statusTextArea.append("CREATE TABLE product(\nproduct_code VARCHAR(8) NOT NULL,\nproduct_name VARCHAR(45),\nproduct_description VARCHAR(50),\nproduct_price DOUBLE(8,2),\nproduct_quantity INTEGER(5),\nPRIMARY KEY(product_code)\n);"); statusTextArea.append("\n\n\n"); statusTextArea.append("CREATE TABLE sales(\nor_count INTEGER(100) NOT NULL,\ncustomer_id VARCHAR(10) NOT NULL,\nproduct_code VARCHAR(10) NOT NULL,\nitem_count INTEGER(100) NOT NULL,\nor_number VARCHAR(10) NOT NULL,\ndate VARCHAR(100) NOT NULL,\ntotal_price DOUBLE(10,2) NOT NULL,\nquantity INTEGER(20),\nPRIMARY KEY (or_count)\n);");

statusTextArea.append("\n\n\n\n"); while(rs.next()){ statusTextArea.append("INSERT INTO customer VALUES\n"); id = rs.getString("customer_id"); last = rs.getString("customer_last"); add = rs.getString("customer_address"); contact = rs.getString("contact_number"); first = rs.getString("customer_first"); ini = rs.getString("customer_ini");

statusTextArea.append("("+id+","+"'"+last+"'"+","+"'"+add+"'"+","+"'"+contact+"'"+","+"'"+first+"'"+","+"'"+ini+"'"+");"); statusTextArea.append("\n"); } statusTextArea.append("\n\n\n"); while(rs2.next()){ statusTextArea.append("INSERT INTO logins VALUES\n"); lu = rs2.getString("loguser"); dt = rs2.getString("date"); statusTextArea.append("('"+lu+"'"+","+"'"+dt+"');"); statusTextArea.append("\n"); } statusTextArea.append("\n\n\n"); while(rs3.next()){ statusTextArea.append("INSERT INTO privilege VALUES\n"); un = rs3.getString("username"); lvl = rs3.getString("level"); statusTextArea.append("('"+un+"'"+","+lvl+");"); statusTextArea.append("\n"); } statusTextArea.append("\n\n\n"); while(rs4.next()){ statusTextArea.append("INSERT INTO product VALUES\n"); pc = rs4.getString("product_code"); pn = rs4.getString("product_name"); pd = rs4.getString("product_description"); pp = rs4.getString("product_price"); pq = rs4.getString("product_quantity"); statusTextArea.append("('"+pc+"'"+","+"'"+pn+"'"+","+"'"+pd+"'"+","+pp+","+pq+");"); statusTextArea.append("\n"); } statusTextArea.append("\n\n\n"); while(rs5.next()){ statusTextArea.append("INSERT INTO sales VALUES\n"); oc = rs5.getString("or_count"); ci = rs5.getString("customer_id"); prc = rs5.getString("product_code"); ic = rs5.getString("item_count"); on = rs5.getString("or_number"); dte = rs5.getString("date"); tp = rs5.getString("total_price"); quantity = rs5.getString("quantity");

statusTextArea.append("("+oc+","+"'"+ci+"'"+","+"'"+prc+"'"+","+ic+","+"'"+on+"'"+","+"'"+dte+"'"+","+tp+","+quantity+");"); statusTextArea.append("\n"); } } catch(Exception e){ JOptionPane.showMessageDialog(null, e.getMessage()); } }//GEN-LAST:event_generateButtonActionPerformed private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_uploadButtonActionPerformed String SQL = statusTextArea.getText(); try{ PreparedStatement stmt = conn.prepareStatement(SQL); ResultSet rs = stmt.executeQuery(); JOptionPane.showMessageDialog(null,"Restoration Successful"); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Restoration Error"); } }//GEN-LAST:event_uploadButtonActionPerformed

public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new guiBackup().setVisible(true); } }); }
// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton generateButton; private javax.swing.JInternalFrame generateInternalFrame; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea statusTextArea; private javax.swing.JButton uploadButton; // End of variables declaration//GEN-END:variables }