0

I am a beginner in Java Programming. I try to practice with working on a Java application and i need to create a different database each time i register a new client.

The window contain a textField where to put the name of the client which will be the same name as my database(Please see attached, the picture of my client window)

Thank you in advance for any review or valuable answer !

Connecter.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;



public class Connecter {
Connection con;

public Connecter(String db) {
    try {
        Class.forName("com.mysql.jdbc.Driver");

    } catch (ClassNotFoundException e) {
        System.err.println(e);
        // pour afficher 1 ere erreur
    }
    try {

        con = DriverManager.getConnection("jdbc:mysql://localhost:8889/", "root", "root");

    } catch (SQLException e) {
        System.err.println(e);
    }

}

Connection obtenirconnexion() {
    return con;

}

public Connection getCon() {
    return con;
}

public void setCon(Connection con) {
    this.con = con;
}

}

Client.java

import java.awt.BorderLayout;

public class Client extends JFrame {

Connection conn;
protected String Cl;
private JPanel contentPane;
private JTextField txtCl;
Connecter Cot = new Connecter(Cl);
java.sql.Statement stm;
int Rs;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        public void run() {
            try {
                Client frame = new Client();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Client() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblClient = new JLabel("Client ");
    lblClient.setBounds(46, 99, 61, 16);
    contentPane.add(lblClient);

    txtCl = new JTextField();
    txtCl.setBounds(157, 93, 134, 28);
    contentPane.add(txtCl);
    txtCl.setColumns(10);

    JButton btnCreate = new JButton("Create");
    btnCreate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String Client = txtCl.getText();
            String requete = "Create database " + Client + "";
            Cot.obtenirconnexion();
            conn = Cot.obtenirconnexion();
            try {
                stm.execute(requete);
              System.out.println("" + Client + " is created");
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    });
    btnCreate.setBounds(21, 180, 117, 29);
    contentPane.add(btnCreate);

}

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You've posted some code and a broad requirement but no specific question, and this may hurt your chances of getting a decent answer. A general rule to remember is that the more specific and clear your question is, usually the more specific and helpful your received answers will be. Please spend just a little more time telling us the details of your problem, the details of your code -- what it does -- and where ***exactly*** you're stuck in that code. – Hovercraft Full Of Eels Aug 03 '16 at 15:20
  • Please have a look at the [tour], the [help] as well as the [how to ask good questions](http://stackoverflow.com/help/how-to-ask) sections for more information on how to improve your question and increase your chances of getting decent help. – Hovercraft Full Of Eels Aug 03 '16 at 15:21
  • Ok ! as far as i can be specific. My question is : why my sql Query "Create database '"+Client+"' doesn't execute. – user5953665 Aug 03 '16 at 15:49
  • One issue -- your stm variable looks like it should be null when you use it suggesting that this code will throw a NullPointerException. If this is the case, then this is critical information that you're not telling us. In fact, what **does** this code do when you run it and try to create your database? – Hovercraft Full Of Eels Aug 03 '16 at 16:54
  • You shouldn't use a Statement anyway -- use a PreparedStatement, and initialize your prepared statement object before using it. For that matter, don't even try to connect the database code to the GUI until you've fully debugged your database code first, something that you've not yet done. – Hovercraft Full Of Eels Aug 03 '16 at 16:55
  • @HovercraftFullOfEels Thank you for your comments ! otherwise i have a bug on Rs=stm.executeQuery("Create database '"+Client+"' "); When i run it, it gives me the GUI to enter the client name,After i get this exception : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. But no database created as i check. – user5953665 Aug 03 '16 at 17:18
  • As I mentioned, it's because stm is null, and you shouldn't be surprised that this is so -- look in your code and show me or yourself where you assign any object to this variable, i.e., `stm = ..... something ....;` Answer -- nowhere. Again you're ahead of yourself here -- first get your database code up and running in isolation, and only then port it to a GUI. – Hovercraft Full Of Eels Aug 03 '16 at 17:56
  • @HovercraftFullOfEels Ok thank you I will try to follow your instructions ! – user5953665 Aug 03 '16 at 18:33

0 Answers0