la clase db de ayudita para el parcial:
import java.sql.*;import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DB {
private Connection cn;
private Statement st;
private ResultSet rs;
public DB(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void conectar(){
try {
this.cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dvd", "root", "");
this.st = cn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void desconectar(){
try {
this.st.close();
this.cn.close();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
public synchronized int getCount(String tabla){
int tmp = 0;
try {
this.conectar();
rs = st.executeQuery("select count(*) from "+ tabla);
if(rs.first()){
tmp = rs.getInt(1);
}
this.desconectar();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
return tmp;
}
public void limpiarBase(){
try {
this.conectar();
this.st.executeUpdate("truncate table dvd");
this.desconectar();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setNuevoDvd(dvd D){
}
public ArrayList getFromDb(String consulta){
ArrayList tmpReturn = new ArrayList();
try {
this.conectar();
this.rs = this.st.executeQuery(consulta);
while(rs.next()){
ArrayList tmpFila = new ArrayList();
for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
tmpFila.add(rs.getObject(i));
}
tmpReturn.add(tmpFila);
}
this.desconectar();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
return tmpReturn;
}
private void setDvdToDb(){
}
}
No hay comentarios:
Publicar un comentario