package jcredito;
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class Jdbmanager {
private String url
= "jdbc:odbc:Archivio";
public Jdbmanager() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
regLog("SQLException: " + ex.getMessage());
}
}
private void regLog
(String msg
) {
try {
f.println("-----------------------------------------------------------------------------------");
f.println("- Data: " + day + "/" + mese + "/" + anno);
f.println("- Ora: " + hh + ":" + mm);
f.println(msg);
f.println("-----------------------------------------------------------------------------------");
Logger.getLogger(Jdbmanager.class.getName()).log(Level.SEVERE, null, ex);
} finally {
f.close();
}
}
public boolean reg(Utente T) {
boolean x = true;
try {
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT Nk FROM Utente");
while (rs.next()) {
if (T.getNk().equals(rs.getString("Nk"))) {
x = false;
}
}
if (x) {
stmt.executeUpdate("INSERT INTO Utente (Nk,Psw,SaldoTot) VALUES ('" + T.getNk() + "','" + T.getPsw() + "','" + T.getCr() + "')");
}
con.close();
regLog("SQLException: " + ex.getMessage());
}
return x;
}
public boolean login(Utente T) {
boolean x = false;
try {
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT Nk,Psw FROM Utente");
while (rs.next()) {
if (T.getNk().equals(rs.getString("Nk")) && T.getPsw().equals(rs.getString("Psw"))) {
x = true;
}
}
con.close();
regLog("SQLException: " + ex.getMessage());
}
return x;
}
try {
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO Manovra (Nkrif,Tipo,Importo,Data,Descrizione) VALUES ('" + user + "','" + tipo + "','" + imp + "','" + data + "','" + des + "')");
if ("Versamento".equals(tipo)) {
stmt.executeUpdate("UPDATE Utente SET SaldoTot = SaldoTot +" + imp + " WHERE Nk = '" + user + "'");
} else {
stmt.executeUpdate("UPDATE Utente SET SaldoTot = SaldoTot -" + imp + " WHERE Nk = '" + user + "'");
}
con.close();
return true;
regLog("SQLException: (Utente: " + user + ") " + ex.getMessage());
return false;
}
}
public boolean delete
(String x
) {//da finire
try {
stmt = con.createStatement();
stmt.executeUpdate("DELETE FROM Utente WHERE Nk = '" + x + "'");
con.close();
return true;
regLog("SQLException: " + ex.getMessage());
return false;
}
}
columnNames.addElement("Tipo manovra");
columnNames.addElement("Importo");
columnNames.addElement("Data");
columnNames.addElement("Descizione");
try {
// Connect to the Database
stmt = con.createStatement();
// Read data from a table
rs = stmt.executeQuery("SELECT Nkrif,Tipo,Importo,Data,Descizione FROM Manovra WHERE Nkrif = '" + user + "' ORDER BY Data");
// Get row data
while (rs.next()) {
row.addElement(rs.getObject("Tipo"));
row.addElement(rs.getObject("Importo"));
row.addElement(rs.getObject("Data"));
row.addElement(rs.getObject("Descrizione"));
data.addElement(row);
}
//ultima riga contiene il saldo totale
rs = null;
rs = stmt.executeQuery("SELECT Nk,SaldoTot FROM Utente WHERE Nk = '" + user + "'");
rs.next();
row.addElement("");
row.addElement(user);
row.addElement(" Totale Importo");
row.addElement(rs.getObject("SaldoTot"));
data.addElement(row);
//
rs.close();
stmt.close();
regLog("SQLException: " + e.getMessage());
}
table.setCellSelectionEnabled(true);
//table.setPreferredScrollableViewportSize(new Dimension(400, 400));
p.
setBackground(Color.
blue);
p.add(scrollPane);
return p;
}
columnNames.addElement("Tipo manovra");
columnNames.addElement("Importo");
columnNames.addElement("Data");
columnNames.addElement("Descizione");
try {
// Connect to the Database
stmt = con.createStatement();
// Read data from a table
rs = stmt.executeQuery("SELECT Nkrif,Tipo,Importo,Data,Descrizione FROM Manovra WHERE Data BETWEEN '" + d1 + "' AND '" + d2 + "' AND Nkrif = '" + user + "' ORDER BY Data");
// Get row data
while (rs.next()) {
row.addElement(rs.getObject("Tipo"));
row.addElement(rs.getObject("Importo"));
row.addElement(rs.getObject("Data"));
row.addElement(rs.getObject("Descrizione"));
data.addElement(row);
}
//ultima riga contiene il saldo totale
rs = null;
rs = stmt.executeQuery("SELECT Nk,SaldoTot FROM Utente WHERE Nk = '" + user + "'");
rs.next();
row.addElement("");
row.addElement(user);
row.addElement(" Totale Importo");
row.addElement(rs.getObject("SaldoTot"));
data.addElement(row);
//
rs.close();
stmt.close();
regLog("SQLException: " + e.getMessage());
}
table.setCellSelectionEnabled(true);
//table.setPreferredScrollableViewportSize(new Dimension(400, 400));
p.
setBackground(Color.
blue);
p.add(scrollPane);
return p;
}
}