Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- new Window();
- }
- }
- import javax.swing.*;
- import java.awt.event.*;
- public class Window extends JFrame {
- private static Database db;
- JPanel panel;
- JLabel lblName;
- JLabel lblLvl;
- JTextField txtName;
- JTextField txtLvl;
- JComboBox<String> boxType;
- JButton btnSave;
- Boolean boxTypeExist;
- public Window() {
- db = new Database();
- Boolean connection;
- do{
- connection = db.connect();
- }while(!connection);
- setTitle("Register");
- setSize(300,70);
- setResizable(false);
- JPanel panel = new JPanel();
- panel.setLayout(null);
- lblName = new JLabel("Name :");
- lblLvl = new JLabel("Level :");
- txtName = new JTextField(12);
- txtLvl = new JTextField(5);
- boxTypeExist = false;
- boxType = new JComboBox<String>(new String[]{"Type","Warrior","Magician","Archer","Assassin","Thief","Yordle"});
- boxType.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- boxType.removeItemAt(0);
- boxTypeExist = true;
- boxType.removeActionListener(this);
- }
- });
- btnSave = new JButton("Save");
- btnSave.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String name = txtName.getText();
- String level = txtLvl.getText();
- if (name.length() > 0)
- if (level.length() > 0)
- if (boxTypeExist){
- String type = boxType.getSelectedItem().toString();
- db.execute("insert into Characters values ('" + name + "'," + level + ",'" + type + "')");
- db.disconnect();
- dispose();
- new Window();
- }
- }
- });
- lblName.setBounds(15,10,50,20);
- lblLvl.setBounds(15,30,50,20);
- txtName.setBounds(75,10,100,20);
- txtLvl.setBounds(75,30,100,20);
- boxType.setBounds(180,10,100,20);
- btnSave.setBounds(180,30,100,20);
- panel.add(lblName);
- panel.add(txtName);
- panel.add(lblLvl);
- panel.add(txtLvl);
- panel.add(boxType);
- panel.add(btnSave);
- getContentPane().add(panel);
- setVisible(true);
- }
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Database {
- // mysql -h localhost -u root -p 1234
- private static Connection connection;
- public static boolean connect() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String dbName = "SavedData";
- String userName = "root";
- String dbPw = "1234";
- Database.connection = DriverManager.getConnection("jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+dbPw);
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public static boolean disconnect() {
- try {
- Database.connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public static ResultSet execute(String sql) {
- Statement exec;
- try {
- exec = Database.connection.createStatement();
- exec.execute(sql);
- return exec.getResultSet();
- } catch (SQLException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement