Guest User

Untitled

a guest
Dec 6th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. public class CasaMarmitasConnection {
  8.  
  9. private static String status = "Não conectado...";
  10.  
  11. public static Connection criarConexao()
  12. throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
  13.  
  14. Connection connection = null;
  15.  
  16. String serverName = "localhost"; // caminho do servidor do BD
  17.  
  18. String mydatabase = "nome-do-banco"; // nome do seu banco de dados
  19.  
  20. String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
  21.  
  22. String username = "usuario"; // nome de um usuário de seu BD
  23.  
  24. String password = "senha"; // sua senha de acesso
  25.  
  26. connection = DriverManager.getConnection(url, username, password);
  27.  
  28. Class.forName("com.mysql.jdbc.Driver").newInstance();
  29.  
  30. //Testa a conexão com o banco
  31. if (connection != null) {
  32. status = ("STATUS: Conectado com sucesso!");
  33. } else {
  34. status = ("STATUS: Não foi possivel realizar conexão");
  35. }
  36.  
  37. return connection;
  38. }
  39.  
  40. public static ResultSet executarSelect(Connection conn, String sql) throws SQLException{
  41. Statement query = conn.createStatement();
  42. return query.executeQuery(sql);
  43. }
  44.  
  45. public static java.sql.PreparedStatement prepararSql(Connection conn, String sql) throws SQLException{
  46. return conn.prepareStatement(sql);
  47. }
  48.  
  49. public static String statusConection() {
  50. return status;
  51. }
  52.  
  53. }
Add Comment
Please, Sign In to add comment