Guest User

Leandro Costa

a guest
Mar 9th, 2009
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. //Database.java
  2. package loki.util;
  3.  
  4. import java.sql.*;
  5. import java.io.Serializable;
  6.  
  7. public class Database implements Serializable{
  8.     private String dbdriver,dburl,dbuser,dbpass;
  9.     private Connection con;
  10.  
  11.     public static final long serialVersionUID=1l;
  12.  
  13. //  Constantes com driver de alguns bancos de dados.
  14.     public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
  15.     public static final String POSTGRES_DRIVER = "org.postgresql.Driver";
  16.  
  17.     public Database(String driver,String url) throws ClassNotFoundException,SQLException{
  18.         this(driver,url,"","");
  19.     }
  20.  
  21.     public Database(String driver,String url,String user,String pass) throws ClassNotFoundException,SQLException{
  22.         dbdriver = driver;
  23.         dburl = url;
  24.         dbuser = user;
  25.         dbpass = pass;
  26.         Class.forName(dbdriver);
  27.         con = DriverManager.getConnection(dburl,dbuser,dbpass);
  28.     }
  29.  
  30.     public void setAutocommit(boolean b) throws SQLException{
  31.         con.setAutoCommit(b);
  32.     }
  33.  
  34.     public boolean getAutocommit() throws SQLException{
  35.         return con.getAutoCommit();
  36.     }
  37.  
  38.     public void commit() throws SQLException{
  39.         con.commit();
  40.     }
  41.  
  42.     public void rollback() throws SQLException{
  43.         con.rollback();
  44.     }
  45.  
  46.     public Connection getConnection(){return con;}
  47.  
  48.     public Statement getStatement() throws SQLException {
  49.         return con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_INSENSITIVE);
  50.     }  
  51.  
  52.     public PreparedStatement prepare(String sql) throws SQLException{
  53.         return con.prepareStatement(sql);
  54.     }
  55.  
  56.     public ResultSet consulta(String sql)  throws SQLException{
  57.         return getStatement().executeQuery(sql);
  58.     }
  59.  
  60.     public void execute(String sql) throws SQLException{
  61.         getStatement().execute(sql);
  62.     }
  63.    
  64.     public void close()  throws SQLException{
  65.         con.close();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment