package com.gmail.dougpts.dao;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.Connection;
public class Conexao {
//Coloque aqui o drive de conexão com o banco de dados
private static String DRIVER = "com.mysql.jdbc.Driver";
//Coloque aqui a URL do banco junto com seu nome
private static String URL = "jdbc:mysql://localhost/teste?zeroDateTimeBehavior=convertToNull";
//Coloque aqui o usuário do Banco de Dados
private static String USUARIO = "root";
//Coloque aqui a senha do Banco de Dados
private static String SENHA = "root";
public static Connection openConnection() {
try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USUARIO, SENHA);
return conn;
} catch (SQLException ex) {
print(ex.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static void closeConnection(Connection con){
try {
con.close();
} catch (SQLException ex) {
print(ex.getMessage());
}
}
public static void insert(String table, String atributos, String valores){
try {
Connection con = openConnection();
PreparedStatement stm;
String sql = "INSERT INTO " + table + " (" + atributos + ") VALUES (" + valores + ");";
stm = con.prepareStatement(sql);
stm.executeUpdate();
closeConnection(con);
} catch (SQLException ex) {
print(ex.getMessage());
}
}
public static void update(String table, String pk, String id, String campos){
try {
Connection con = openConnection();
PreparedStatement stm;
String sql = "UPDATE " + table + " SET " + campos + " WHERE " + pk + " = '" + id + "'";
stm = con.prepareStatement(sql);
stm.executeUpdate();
closeConnection(con);
} catch (SQLException ex) {
print(ex.getMessage());
}
}
public static void delete(String table, String condicoes){
try {
Connection con = openConnection();
String sql = "DELETE FROM " + table + " WHERE " + condicoes;
PreparedStatement stm = con.prepareStatement(sql);
stm.executeUpdate();
closeConnection(con);
} catch (SQLException ex) {
print(ex.getMessage());
}
}
public static boolean isExist(String tabela, String nomeColuna, String where){
try {
String query = "SELECT COUNT(" + nomeColuna + ") AS CONT FROM " + tabela + " WHERE " + where;
CachedRowSet result = Conexao.getRowSet(query);
if (result.next()) {
int cont = Integer.parseInt(result.getString("CONT"));
if (cont > 0) {
return true;
}
}
} catch (SQLException ex) {
print(ex.getMessage());
}
return false;
}
public static CachedRowSet getRowSet(String sql){
try {
Connection con = openConnection();
PreparedStatement stm = con.prepareStatement(sql);
ResultSet rs = stm.executeQuery();
CachedRowSet result = new CachedRowSetImpl();
result.populate(rs);
rs.close();
closeConnection(con);
return result;
} catch (SQLException ex) {
print(ex.getMessage());
}
return null;
}
public static void run(String sql){
try {
Connection con = openConnection();
PreparedStatement stm;
System.out.println(sql);
stm = con.prepareStatement(sql);
stm.executeUpdate();
closeConnection(con);
} catch (SQLException ex) {
print(ex.getMessage());
}
}
private static void print(String message){
System.out.print(message);
}
}
quinta-feira, 12 de novembro de 2009
Post 2 - Conexão
Assinar:
Postar comentários (Atom)

Nenhum comentário:
Postar um comentário