In my project there are a lot of GUI so I have a class for each GUI. I created a class to connect to my db (MySQL). Remember, I don't have problems with code not working. The code (important to this question) in the connectiondb
class is:
public static void loadDriver_doConnection(){
try {
Class.forName(jdbc_driver);
try { conn = DriverManager.getConnection(url1,"root","pswww");
}
catch(SQLException es)
{ //es.printStackTrace();
}
}
catch (ClassNotFoundException ex) {
//ex.printStackTrace();
}
}
public static Connection loadDriver_doConnection_backConn(){
try {
Class.forName(jdbc_driver);
try { conn = DriverManager.getConnection(url1,"root","pswww");
}
catch(SQLException es)
{ //es.printStackTrace();
}
}
catch (ClassNotFoundException ex) {
//ex.printStackTrace();
}
return conn;
}
static ResultSet doQuery2(String sql, boolean full_navigable)
{
if (conn != null)
{
try //
{
if (full_navigable)
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
else
st = conn.createStatement();
rs = st.executeQuery(sql);
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
}
return rs;
}
else
{
return null;
}
And now I'm showing you an example (with no try-catch to control input) of my code on a GUI where I do an insert/delete:
String sqlinsart="insert into Art (CF,Name,Surname,Date,City born,City live,road,Num,Email,Tel,Cell) values(?,?,?,?,?,?,?,?,?,?,?)";
Connection conn=Connectiondb.loadDriver_doConnection_backConn();
PreparedStatement ps = conn.prepareCall(sqlinsart);
ps.setString(1, txtformattedcf.getText().trim());
ps.setString(2, txtname.getText().trim());
ps.setString(3, txtsurname.getText().trim());
SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date invoiceDate = formatDate.parse(txtformatteddate.getText().trim());
java.sql.Date sqlDate = new java.sql.Date(invoiceDate.getTime());
ps.setDate(4, sqlDate);
ps.setString(5, txtcityb.getText().trim());
ps.setString(6, txtcity.getText().trim());
ps.setString(7, txtroad.getText().trim());
ps.setString(8, txtformattednum.getText().trim());
ps.setString(11, txtemail.getText().trim());
ps.setString(12, txtformattedtel.getText().trim());
ps.setString(13, txtformattedcell.getText().trim());
int n = ps.executeUpdate();
if (n>0){
JOptionPane.showMessageDialog(null, "Insert ok");
}
DELETE (these actions are done by bottons):
String userelimart=txtformattedcf.getText().trim();
String sqlelimart="delete from Art where Art.CF='"+userelimart+"'";
Connection conn=Connectiondb.loadDriver_doConnection_backConn();
Statement sent=conn.createStatement();
int n =sent.executeUpdate(sqlelimart);
if (n>0){
JOptionPane.showMessageDialog(null,"delete ok");
The update is similar. So, in this way, I use the doQuery2
method of Connectiondb.class
only for the try-catch control of input, for example:
String art=txtformattedcf.getText().trim();
String sqluss="select CF from Art where Art.CF='"+art+"'";
ResultSet rsuss=Connectiondb.doQuery2(sqluss,true);
if (rsuss.next()==false){
throw new SQLException();
}
I use the second parameter when I have to put the data in jtable etc., but it's not important here.
Is there a better way to manage all of this, such as adding other methods in Connectiondb
? I use these insert/delete in the GUIs many times and I think management is not efficient, maybe because I didn't create the classes for the date. For example, a class to take the information of a person (with get, set) of a company or what I need. I didn't do that because I treat the data directly to the database.
For example, is there a way to do this code
Connection conn=Connectiondb.loadDriver_doConnection_backConn();
Statement sent=conn.createStatement();
int n =sent.executeUpdate(sqlelimart);
if (n>0){
JOptionPane.showMessageDialog(null,"delete ok");
inside of a method Connectiondb
where I give the SQL String (without then rewrite this code every time in each GUI)? I created the method Connectiondb.loadDriver_doConnection_backConn()
because I always treat the data in the buttons GUI and not in the Connectiondb
class. This is also the same for the insert and update, but now I can't find a right way.
For the preparedStatement
method I'm writing this code:
static void SetString(PreparedStatement ps, Integer[] indexstring, JTextField[] textstring){
for (int i=0;i<indexstring.length;i++)
try {
ps.setString(indexstring[i], textstring[i].getText().trim());
} catch (SQLException e) {
//msg with error
}
}
static void SetInt(PreparedStatement ps, Integer[] indexint, JTextField[] textint){
for (int i=0;i<indexint.length;i++)
try {
ps.setInt(indexint[i], Integer.parseInt(textint[i].getText().trim()));
} catch (SQLException e) {
//msg with error
}
}
static void SetDouble(PreparedStatement ps, Integer[] indexdouble, JTextField[] textdouble){
//etc
}
static void SetDate(PreparedStatement ps, Integer[] indexdate, JTextField[] textdate){
//etc
}
There is a better way to do this? (In this way I have to write other 4 methods for the JFormattedTextField
)