refactoring funzioni, metodi e variabili

creazione file Utils
This commit is contained in:
Francesco Di Sciascio 2024-05-09 11:23:23 +02:00
parent f7f5a3975e
commit 086dc95e49
2 changed files with 324 additions and 245 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,103 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package puntocassa.utils;
import java.sql.*;
import javax.swing.JOptionPane;
import puntocassa.PuntoCassa;
/**
*
* @author Francesco DS
*/
public class Utils {
public static boolean isNumeric(String s) {
return s.matches("[-+]?\\d*\\.?\\d+");
}
public static String mySelect(String query, String campo, String dbStringa, String dbUsername, String dbPassword, PuntoCassa puntoCassa) {
String res = "";
try {
Connection dbConnection = DriverManager.getConnection(dbStringa, dbUsername, dbPassword);
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
res = rs.getString(campo);
}
rs.close();
st.close();
dbConnection.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(puntoCassa, "Errore MySelect ['" + e.getMessage() + " " + query + "']");
}
if (res == null) {
res = "";
}
return res;
}
public static Boolean myInsert(String query, String dbStringa, String dbUsername, String dbPassword, PuntoCassa puntoCassa) {
//System.out.println(query);
Boolean res = false;
try {
Connection dbConnection = DriverManager.getConnection(dbStringa, dbUsername, dbPassword);
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery(query);
rs.close();
st.close();
dbConnection.close();
res = true;
} catch (Exception e) {
JOptionPane.showMessageDialog(puntoCassa, "Errore MySelect ['" + e.getMessage() + " " + query + "']");
}
return res;
}
public static Long mySelectInteger(String query, String campo, String dbStringa, String dbUsername, String dbPassword, PuntoCassa puntoCassa) {
System.out.println(query);
Long res = 0L;
try {
Connection dbConnection = DriverManager.getConnection(dbStringa, dbUsername, dbPassword);
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
res = rs.getLong(campo);
}
rs.close();
st.close();
dbConnection.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(puntoCassa, "Errore MySelectInteger ['" + e.getMessage() + " " + query + "']");
}
return res;
}
public static String spaziBianchi(String testo, Integer Num, Boolean suffisso) {
String res = testo;
StringBuilder suff = new StringBuilder();
if (testo.length() < Num) {
Integer delta = Num - testo.length();
for (Integer i = 0; i < delta; i++) {
suff.append(" ");
}
if (suffisso == false) {
res = suff.toString() + testo;
} else {
res = testo + suff.toString();
}
}
return res;
}
}