Scusate, non ho mai usato forum. Spero che cosi vada bene. Comunque il mio dilemma è come fare queste insert chiedendo i dati all'utente e poi richiamare il tutto, incluso questa query già fatta, tramite lo switch case.
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
public class ProgettoCanile {
private static List<Connection> freeDbConnections;
static {
freeDbConnections = new LinkedList<Connection>();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("DB driver not found!"+ e);
}
}
private static Connection createDBConnection() throws SQLException
{
Connection newConnection = null;
String ip = "localhost";
String port = "3306";
String db = "canile";
String username = "root";
String password = "basidati";
newConnection = DriverManager.getConnection("jdbc:mysql://"+ ip+":"+ port+"/"+db, username, password);
newConnection.setAutoCommit(false);
return newConnection;
}
public static synchronized Connection getConnetion() throws SQLException
{
Connection connection;
if(! freeDbConnections.isEmpty()) {
connection = (Connection) freeDbConnections.get(0);
ProgettoCanile.freeDbConnections.remove(0);
try {
if (connection.isClosed())
connection = ProgettoCanile.getConnetion();
} catch (SQLException e) {
if(connection != null)
connection.close();
connection = ProgettoCanile.getConnetion();
}
} else connection = ProgettoCanile.createDBConnection();
return connection;
}
public static synchronized void releaseConnection(Connection connection) {
ProgettoCanile.freeDbConnections.add(connection);
}
public static void main(String args[]) throws Exception {
try {
String url = "jdbc:mysql://localhost:3306/canile";
Connection con = DriverManager.getConnection(url,"root", "basidati");
System.out.println("Connessione OK \n");
Statement st = con.createStatement();
String sql = "SELECT * FROM Cane";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
String Microchip = rs.getString("microchip");
int AnnoN = rs.getInt("annon");
String Sesso = rs.getString("sesso");
String Taglia = rs.getString("taglia");
String Colore = rs.getString("colore");
String Pelo = rs.getString("pelo");
String SegniP = rs.getString("segnip");
System.out.printf("%s , %d , %s , %s , %s , %s , %s \n", Microchip, AnnoN, Sesso, Taglia, Colore, Pelo, SegniP);
}
if (rs != null) rs.close();
if(st != null) st.close();
con.close();
}
catch(Exception e) {
System.out.println("Connessione fallita \n");
System.out.println(e);
}
try {
String url = "jdbc:mysql://localhost:3306/canile";
Connection con = DriverManager.getConnection(url,"root", "basidati");
System.out.println("Connessione OK \n");
Statement st = con.createStatement();
String sql = "SELECT microchip,annon FROM Cane";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
String Microchip = rs.getString("microchip");
int AnnoN = rs.getInt("annon");
System.out.printf("%s , %d \n", Microchip, AnnoN);
}
if (rs != null) rs.close();
if(st != null) st.close();
con.close();
}
catch(Exception e) {
System.out.println("Connessione fallita \n");
System.out.println(e);
}
}
}