Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Database.java
- package loki.util;
- import java.sql.*;
- import java.io.Serializable;
- public class Database implements Serializable{
- private String dbdriver,dburl,dbuser,dbpass;
- private Connection con;
- public static final long serialVersionUID=1l;
- // Constantes com driver de alguns bancos de dados.
- public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
- public static final String POSTGRES_DRIVER = "org.postgresql.Driver";
- public Database(String driver,String url) throws ClassNotFoundException,SQLException{
- this(driver,url,"","");
- }
- public Database(String driver,String url,String user,String pass) throws ClassNotFoundException,SQLException{
- dbdriver = driver;
- dburl = url;
- dbuser = user;
- dbpass = pass;
- Class.forName(dbdriver);
- con = DriverManager.getConnection(dburl,dbuser,dbpass);
- }
- public void setAutocommit(boolean b) throws SQLException{
- con.setAutoCommit(b);
- }
- public boolean getAutocommit() throws SQLException{
- return con.getAutoCommit();
- }
- public void commit() throws SQLException{
- con.commit();
- }
- public void rollback() throws SQLException{
- con.rollback();
- }
- public Connection getConnection(){return con;}
- public Statement getStatement() throws SQLException {
- return con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_INSENSITIVE);
- }
- public PreparedStatement prepare(String sql) throws SQLException{
- return con.prepareStatement(sql);
- }
- public ResultSet consulta(String sql) throws SQLException{
- return getStatement().executeQuery(sql);
- }
- public void execute(String sql) throws SQLException{
- getStatement().execute(sql);
- }
- public void close() throws SQLException{
- con.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment