Advertisement
TermSpar

Add SQL Data

Mar 23rd, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package jdbcTest;
  2. import java.awt.BorderLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.*;
  6.  
  7. import javax.swing.*;
  8. public class Driver extends JFrame {
  9.      
  10.    
  11.     //DEFINE VARIABLES
  12.     private static JTextField userText;
  13.     private JTextField serverConnection;
  14.     private JTextField userName;
  15.     private JTextField passWord;
  16.     private JButton btn;
  17.     private JLabel lbl;
  18.     private JLabel lblServer;
  19.     private JLabel lblEnterData;
  20.     private JLabel lblUsername;
  21.     private JLabel lblPassword;
  22.    
  23.     public Driver(){
  24.         super("SQL Test");
  25.         userText = new JTextField();
  26.         userName = new JTextField();
  27.         passWord = new JTextField();
  28.         serverConnection = new JTextField();
  29.        
  30.         lbl = new JLabel();
  31.         lblServer = new JLabel();
  32.         lblEnterData = new JLabel();
  33.         lblUsername = new JLabel();
  34.         lblPassword = new JLabel();
  35.        
  36.        
  37.         btn = new JButton();
  38.         btn.addActionListener(
  39.                 new ActionListener(){
  40.                     public void actionPerformed(ActionEvent e){
  41.                        
  42.                         String sqlQuery = userText.getText();
  43.                         String realSQL = "INSERT INTO realtest "
  44.                                 + "VALUES"
  45.                                 + "("
  46.                                 + "'"
  47.                                 + sqlQuery
  48.                                 + "'"
  49.                                 + ")";
  50.                         String serverURL = serverConnection.getText();
  51.                         String usernameData = userName.getText();
  52.                         String passwordData = passWord.getText();
  53.                        
  54.                        
  55.                         try{
  56.                            
  57.                             //CONNECT TO DATABASE
  58.                             Connection myConn = DriverManager.getConnection(serverURL, usernameData, passwordData);
  59.                            
  60.                             //MY SQL SERVER URL: jdbc:mysql://127.0.0.1:3306/test USER: root PASS: admin
  61.                            
  62.                             //CREATE STATEMENT
  63.                             Statement mySmt = myConn.prepareStatement(realSQL);
  64.                             JOptionPane.showMessageDialog(null, "DATA INSERTED TO: " + serverURL);
  65.                            
  66.                             //EXECUTE SQL QUERY
  67.                             mySmt.executeUpdate(realSQL);
  68.                         }catch (Exception exc){
  69.                             exc.printStackTrace();
  70.                             JOptionPane.showMessageDialog(null, exc.getMessage());
  71.                         }
  72.                     }
  73.                 }
  74.             );
  75.        
  76.         //CONSTRUCTOR (why I hate java GUIs)
  77.         setLayout(null);
  78.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79.        
  80.         lblServer.setText("SQL SERVER URL:");
  81.         lblServer.setBounds(12, -3, 365, 25);
  82.         add(lblServer);
  83.        
  84.         serverConnection.setText("jdbc:mysql://");
  85.         serverConnection.setBounds(12, 20, 365, 25);
  86.         add(serverConnection);
  87.        
  88.         lbl.setText("Made By Ben Bollinger");
  89.         lbl.setBounds(3, 313, 200, 100);
  90.         add(lbl);
  91.        
  92.         pack();
  93.         setLocation(750, 300);
  94.         setResizable(false);
  95.        
  96.         btn.setText("INSERT DATA");
  97.         btn.setBounds(90, 150, 200, 40);
  98.         add(btn);
  99.        
  100.         lblEnterData.setText("DATA:");
  101.         lblEnterData.setBounds(12, 42, 365, 25);
  102.         add(lblEnterData);
  103.        
  104.         userText.setBounds(12, 65, 365, 25);
  105.         add(userText);
  106.        
  107.         userName.setBounds(12, 300, 100, 25);
  108.         add(userName);
  109.        
  110.         passWord.setBounds(282, 300, 100, 25);
  111.         add(passWord);
  112.        
  113.         lblUsername.setText("Username:");
  114.         lblUsername.setBounds(12, 280, 100, 25);
  115.         add(lblUsername);
  116.        
  117.         lblPassword.setText("Password:");
  118.         lblPassword.setBounds(282, 280, 100, 25);
  119.         add(lblPassword);
  120.        
  121.         setSize(400,400);
  122.         setVisible(true);
  123.     }
  124.     public static void main(String[] args) {
  125.         //EXECUTE CODE
  126.         new Driver();
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement