Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javasql;
- import java.sql.*;
- public class JavaSQL {
- private static String url = "jdbc:mysql://localhost:3306/javasql";
- private static String driver = "com.mysql.jdbc.Driver";
- private static Connection cnx = null;
- protected static void conectar() {
- if (cnx == null) {
- System.out.println("Conectando...");
- try {
- Class.forName(driver);
- cnx = DriverManager.getConnection(url, "root", "");
- } catch (SQLException e) {
- System.out.println(e.getMessage() + ". Error de Conexion CE02");
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage() + ". Error de Conexion CE01");
- } finally {
- if (cnx != null) System.out.println("Conectado con exito!!");
- }
- }
- }
- protected static void desconectar() {
- if (cnx != null) {
- System.out.println("Desconectando...");
- try {
- cnx.close();
- cnx = null;
- } catch (SQLException e) {
- System.out.println(e.getMessage() + ". Error de Desconexion DC01");
- } finally {
- if (cnx == null) System.out.println("Desconectado con exito!!");
- }
- }
- }
- public static void main(String[] args) {
- conectar();
- }
- }
- create database javasql;
- use javasql;
- create table usuario (
- id int not null auto_increment primary key,
- username varchar(125) unique,
- password varchar(125),
- created_at datetime
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement