Created
February 1, 2013 19:20
-
-
Save viniciusde/4693427 to your computer and use it in GitHub Desktop.
Example data class in java using mysql database. Methods to insert, update, delete and select.
/**
Exemplo de classe de dados em java usando banco de dados mysql. Metódos para inserção , update, delete e select.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Data; | |
import java.sql.*; | |
public class BaseData { | |
private Connection objConexao; | |
protected void fecharConexao() throws SQLException { | |
if(objConexao != null && (!objConexao.isClosed())){ | |
objConexao.close(); | |
} | |
} | |
//INSERT, UPDATE and DELETE | |
protected int executeUpdate(StringBuilder prmObjQuery){ | |
int resultado = 0; | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
objConexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "user", "pass"); | |
Statement objComando = objConexao.createStatement(); | |
resultado = objComando.executeUpdate(prmObjQuery.toString()); | |
fecharConexao(); | |
} catch (Exception objExcecao) { | |
} | |
return resultado; | |
} | |
//SELECT | |
protected ResultSet executeQuery(StringBuilder prmObjQuery){ | |
ResultSet objResultado = null; | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
objConexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "user", "pass"); | |
Statement objComando = objConexao.createStatement(); | |
objResultado = objComando.executeQuery(prmObjQuery.toString()); | |
} catch (Exception objExcecao) { | |
} | |
return objResultado; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment