Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package connection;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- //import java.util.logging.Level;
- //import java.util.logging.Logger;
- public class TesteBanco {
- private static final String DRIVER = "com.mysql.jdbc.Driver";
- private static final String URL = "jdbc:mysql://localhost:3306/pizzaria";
- private static final String USER = "root";
- private static final String PASS = "root";
- public static Connection getConnection(){
- try {
- Class.forName(DRIVER);
- return DriverManager.getConnection(URL, USER, PASS); //.getConnection(URL, USER, PASS);
- } catch (ClassNotFoundException | SQLException ex) {
- //Logger.getLogger(TesteBanco.class.getName()).log(Level.SEVERE, null, ex);
- throw new RuntimeException("Erro na conecxão com o banco", ex);
- }
- }
- public static void CloseConnection(Connection con){
- if(con != null){
- try {
- con.close();
- } catch (SQLException ex) {
- //Logger.getLogger(TesteBanco.class.getName()).log(Level.SEVERE, null, ex);
- System.err.println("Erro: "+ex);
- }
- }
- }
- public static void CloseConnection(Connection con, PreparedStatement statement){
- if(statement != null){
- try {
- statement.close();
- } catch (SQLException ex) {
- System.err.println("Erro: "+ex); }
- }
- CloseConnection(con);
- }
- public static void CloseConnection(Connection con, PreparedStatement statement, ResultSet rs){
- if(rs != null){
- try {
- rs.close();
- } catch (SQLException ex) {
- System.err.println("Erro: "+ex);
- }
- }
- CloseConnection(con, statement);
- }
- public static void main(String []args){
- getConnection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement