Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.12 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package temp__pro;
  7.  
  8. import java.sql.*;
  9. import java.sql.Connection;
  10.  
  11. /**
  12.  *
  13.  * @author WizardMan
  14.  */
  15. public class Conect {
  16.  
  17. Connection conn = null;
  18.  
  19. public void Conectate() throws ClassNotFoundException {
  20.   try{
  21.     Class.forName("com.mysql.jdbc.Driver");
  22.     conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/bjm_db", "root", "root");
  23.     if(conn!=null){
  24.     System.out.println(" Connection to DB successful");
  25.          }
  26.      }
  27. catch(SQLException e){
  28. System.out.println(e);
  29. }
  30.  
  31.     }
  32. public Connection getConnection(){
  33. return conn;
  34. }
  35. public void disconnect(){
  36. conn=null;
  37. }
  38.  
  39.     }
  40. ---------------------------------------------------------------
  41.  
  42. /*
  43.  * To change this template, choose Tools | Templates
  44.  * and open the template in the editor.
  45.  */
  46.  
  47. package temp__pro;
  48.  
  49. import java.sql.SQLException;
  50. import java.util.logging.Level;
  51. import java.util.logging.Logger;
  52. import java.sql.Connection;
  53. import java.sql.ResultSet;
  54. import java.sql.DriverManager;
  55. import java.sql.PreparedStatement;
  56.  
  57. /**
  58.  *
  59.  * @author WizardMan
  60.  */
  61. public class DB_cl {
  62.  
  63.     Connection conn=null;
  64.  //con = new Conect();
  65.  Conect c = new Conect();
  66.  
  67.  
  68.  //constructor
  69.     public DB_cl()
  70.     {
  71.         super();
  72.         try {
  73.             c.Conectate();
  74.             // c.Conectate();
  75.         } catch (ClassNotFoundException ex) {
  76.             Logger.getLogger(DB_cl.class.getName()).log(Level.SEVERE, null, ex);
  77.         }
  78.        
  79.     }
  80.  
  81.     public void insert ( String q) throws SQLException{
  82.  
  83.  
  84.         PreparedStatement pstm = (PreparedStatement) c.getConnection().prepareStatement(q);
  85.         pstm.execute(q);
  86.         c.disconnect();
  87.        
  88.  
  89.     }
  90. }
  91.  
  92. ---------------------------------------------------------------------------
  93. /*
  94.  * To change this template, choose Tools | Templates
  95.  * and open the template in the editor.
  96.  */
  97.  
  98. package temp__pro;
  99.  
  100. /**
  101.  *
  102.  * @author WizardMan
  103.  */
  104. public class Main {
  105.  
  106.     /**
  107.      * @param args the command line arguments
  108.      */
  109.     public static void main(String[] args) {
  110.  
  111.  
  112.         java.awt.EventQueue.invokeLater(new Runnable() {
  113.             public void run() {
  114.                 new registration().setVisible(true);
  115.             }
  116.         });
  117.     }
  118.         // TODO code application logic here
  119.     }
  120.  
  121.  
  122. -----------------------------------------------------------------------------
  123.  
  124. /*
  125.  * To change this template, choose Tools | Templates
  126.  * and open the template in the editor.
  127.  */
  128.  
  129. /*
  130.  * registration.java
  131.  *
  132.  * Created on Dec 4, 2010, 7:11:02 PM
  133.  */
  134.  
  135. package temp__pro;
  136.  
  137. import java.sql.SQLException;
  138. import java.util.logging.Level;
  139. import java.util.logging.Logger;
  140.  
  141. /**
  142.  *
  143.  * @author WizardMan
  144.  */
  145. public class registration extends javax.swing.JFrame {
  146.  
  147.  
  148.     DB_cl j = new DB_cl();
  149.  
  150.     /** Creates new form registration */
  151.     public registration() {
  152.         initComponents();
  153.     }
  154.  
  155.     /** This method is called from within the constructor to
  156.      * initialize the form.
  157.      * WARNING: Do NOT modify this code. The content of this method is
  158.      * always regenerated by the Form Editor.
  159.      */
  160.     @SuppressWarnings("unchecked")
  161.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  162.     private void initComponents() {
  163.  
  164.         first = new javax.swing.JTextField();
  165.         last = new javax.swing.JTextField();
  166.         user = new javax.swing.JTextField();
  167.         passwd = new javax.swing.JTextField();
  168.         jLabel1 = new javax.swing.JLabel();
  169.         jLabel2 = new javax.swing.JLabel();
  170.         jLabel3 = new javax.swing.JLabel();
  171.         jLabel4 = new javax.swing.JLabel();
  172.         jButton1 = new javax.swing.JButton();
  173.  
  174.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  175.  
  176.         jLabel1.setText("First");
  177.  
  178.         jLabel2.setText("Last");
  179.  
  180.         jLabel3.setText("Usr");
  181.  
  182.         jLabel4.setText("pass");
  183.  
  184.         jButton1.setText("Submit");
  185.         jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
  186.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  187.                 jButton1MouseClicked(evt);
  188.             }
  189.         });
  190.  
  191.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  192.         getContentPane().setLayout(layout);
  193.         layout.setHorizontalGroup(
  194.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  195.             .addGroup(layout.createSequentialGroup()
  196.                 .addContainerGap()
  197.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  198.                     .addGroup(layout.createSequentialGroup()
  199.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  200.                             .addComponent(jLabel1)
  201.                             .addComponent(jLabel2)
  202.                             .addComponent(jLabel3))
  203.                         .addGap(12, 12, 12))
  204.                     .addGroup(layout.createSequentialGroup()
  205.                         .addComponent(jLabel4)
  206.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
  207.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  208.                     .addGroup(layout.createSequentialGroup()
  209.                         .addGap(10, 10, 10)
  210.                         .addComponent(jButton1))
  211.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  212.                         .addComponent(first, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
  213.                         .addComponent(last)
  214.                         .addComponent(user)
  215.                         .addComponent(passwd)))
  216.                 .addContainerGap(364, Short.MAX_VALUE))
  217.         );
  218.         layout.setVerticalGroup(
  219.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  220.             .addGroup(layout.createSequentialGroup()
  221.                 .addGap(60, 60, 60)
  222.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  223.                     .addComponent(first, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  224.                     .addComponent(jLabel1))
  225.                 .addGap(18, 18, 18)
  226.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  227.                     .addComponent(last, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  228.                     .addComponent(jLabel2))
  229.                 .addGap(18, 18, 18)
  230.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  231.                     .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  232.                     .addComponent(jLabel3))
  233.                 .addGap(18, 18, 18)
  234.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  235.                     .addComponent(passwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  236.                     .addComponent(jLabel4))
  237.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  238.                 .addComponent(jButton1)
  239.                 .addContainerGap(161, Short.MAX_VALUE))
  240.         );
  241.  
  242.         pack();
  243.     }// </editor-fold>                        
  244.  
  245.     private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
  246.  
  247.         String f = first.getText();
  248.         String l = last.getText();
  249.         String u = user.getText();
  250.         String p = passwd.getText();
  251.  
  252.         //String sql = "insert into pro(firstname, lastname,username, password) values (f,l,u,p)";
  253.         try {
  254.             j.insert("insert into " +
  255. "pro(firstname, lastname, username, password) " +
  256. " values(f,l,u,p)");
  257.             // TODO add your handling code here:
  258.         } catch (SQLException ex) {
  259.             Logger.getLogger(registration.class.getName()).log(Level.SEVERE, null, ex);
  260.         }
  261.     }                                    
  262.  
  263.     /**
  264.     * @param args the command line arguments
  265.     */
  266.     public static void main(String args[]) {
  267.         java.awt.EventQueue.invokeLater(new Runnable() {
  268.             public void run() {
  269.                 new registration().setVisible(true);
  270.             }
  271.         });
  272.     }
  273.  
  274.     // Variables declaration - do not modify                    
  275.     private javax.swing.JTextField first;
  276.     private javax.swing.JButton jButton1;
  277.     private javax.swing.JLabel jLabel1;
  278.     private javax.swing.JLabel jLabel2;
  279.     private javax.swing.JLabel jLabel3;
  280.     private javax.swing.JLabel jLabel4;
  281.     private javax.swing.JTextField last;
  282.     private javax.swing.JTextField passwd;
  283.     private javax.swing.JTextField user;
  284.     // End of variables declaration                  
  285.  
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement