Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbcTest;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.*;
- import javax.swing.*;
- public class Driver extends JFrame {
- //DEFINE VARIABLES
- private static JTextField userText;
- private JTextField serverConnection;
- private JTextField userName;
- private JTextField passWord;
- private JButton btn;
- private JLabel lbl;
- private JLabel lblServer;
- private JLabel lblEnterData;
- private JLabel lblUsername;
- private JLabel lblPassword;
- public Driver(){
- super("SQL Test");
- userText = new JTextField();
- userName = new JTextField();
- passWord = new JTextField();
- serverConnection = new JTextField();
- lbl = new JLabel();
- lblServer = new JLabel();
- lblEnterData = new JLabel();
- lblUsername = new JLabel();
- lblPassword = new JLabel();
- btn = new JButton();
- btn.addActionListener(
- new ActionListener(){
- public void actionPerformed(ActionEvent e){
- String sqlQuery = userText.getText();
- String realSQL = "INSERT INTO realtest "
- + "VALUES"
- + "("
- + "'"
- + sqlQuery
- + "'"
- + ")";
- String serverURL = serverConnection.getText();
- String usernameData = userName.getText();
- String passwordData = passWord.getText();
- try{
- //CONNECT TO DATABASE
- Connection myConn = DriverManager.getConnection(serverURL, usernameData, passwordData);
- //MY SQL SERVER URL: jdbc:mysql://127.0.0.1:3306/test USER: root PASS: admin
- //CREATE STATEMENT
- Statement mySmt = myConn.prepareStatement(realSQL);
- JOptionPane.showMessageDialog(null, "DATA INSERTED TO: " + serverURL);
- //EXECUTE SQL QUERY
- mySmt.executeUpdate(realSQL);
- }catch (Exception exc){
- exc.printStackTrace();
- JOptionPane.showMessageDialog(null, exc.getMessage());
- }
- }
- }
- );
- //CONSTRUCTOR (why I hate java GUIs)
- setLayout(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- lblServer.setText("SQL SERVER URL:");
- lblServer.setBounds(12, -3, 365, 25);
- add(lblServer);
- serverConnection.setText("jdbc:mysql://");
- serverConnection.setBounds(12, 20, 365, 25);
- add(serverConnection);
- lbl.setText("Made By Ben Bollinger");
- lbl.setBounds(3, 313, 200, 100);
- add(lbl);
- pack();
- setLocation(750, 300);
- setResizable(false);
- btn.setText("INSERT DATA");
- btn.setBounds(90, 150, 200, 40);
- add(btn);
- lblEnterData.setText("DATA:");
- lblEnterData.setBounds(12, 42, 365, 25);
- add(lblEnterData);
- userText.setBounds(12, 65, 365, 25);
- add(userText);
- userName.setBounds(12, 300, 100, 25);
- add(userName);
- passWord.setBounds(282, 300, 100, 25);
- add(passWord);
- lblUsername.setText("Username:");
- lblUsername.setBounds(12, 280, 100, 25);
- add(lblUsername);
- lblPassword.setText("Password:");
- lblPassword.setBounds(282, 280, 100, 25);
- add(lblPassword);
- setSize(400,400);
- setVisible(true);
- }
- public static void main(String[] args) {
- //EXECUTE CODE
- new Driver();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement