Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. package javasql;
  2.  
  3. import java.sql.*;
  4.  
  5. public class JavaSQL {
  6.  
  7. private static String url = "jdbc:mysql://localhost:3306/javasql";
  8. private static String driver = "com.mysql.jdbc.Driver";
  9.  
  10. private static Connection cnx = null;
  11.  
  12. protected static void conectar() {
  13. if (cnx == null) {
  14. System.out.println("Conectando...");
  15. try {
  16. Class.forName(driver);
  17. cnx = DriverManager.getConnection(url, "root", "");
  18. } catch (SQLException e) {
  19. System.out.println(e.getMessage() + ". Error de Conexion CE02");
  20. } catch (ClassNotFoundException e) {
  21. System.out.println(e.getMessage() + ". Error de Conexion CE01");
  22. } finally {
  23. if (cnx != null) System.out.println("Conectado con exito!!");
  24. }
  25. }
  26. }
  27.  
  28. protected static void desconectar() {
  29. if (cnx != null) {
  30. System.out.println("Desconectando...");
  31.  
  32. try {
  33. cnx.close();
  34. cnx = null;
  35. } catch (SQLException e) {
  36. System.out.println(e.getMessage() + ". Error de Desconexion DC01");
  37. } finally {
  38. if (cnx == null) System.out.println("Desconectado con exito!!");
  39. }
  40. }
  41. }
  42.  
  43. public static void main(String[] args) {
  44. conectar();
  45. }
  46.  
  47. }
  48.  
  49. create database javasql;
  50.  
  51. use javasql;
  52.  
  53. create table usuario (
  54. id int not null auto_increment primary key,
  55. username varchar(125) unique,
  56. password varchar(125),
  57. created_at datetime
  58. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement