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);
}
}