Script para criar banco de dados
01 CREATE DATABASE JAVA;
02
03 USE MYSQL;
04 DELETE FROM USER WHERE USER = "teste";
05 INSERT INTO USER ( USER, PASSWORD , HOST ) VALUES ("teste", PASSWORD("123"), "localhost" );
06 INSERT INTO USER ( USER, PASSWORD , HOST ) VALUES ("teste", PASSWORD("123"), "%" );
07 FLUSH PRIVILEGES;
08
09 GRANT ALL ON JAVA.* TO TESTE;
10 FLUSH PRIVILEGES;
11
12 USE JAVA;
13
14 CREATE TABLE CLIENTE (
15 ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
16 NOME VARCHAR(30)
17 );
18
19 INSERT INTO CLIENTE ( NOME ) VALUES ('joao');
20 INSERT INTO CLIENTE ( NOME ) VALUES ('jose');
21 INSERT INTO CLIENTE ( NOME ) VALUES ('pedro');
IncluirBanco.java
01 package br.com.thz.samples.db;
02
03 import java.sql.Connection;
04 import java.sql.DriverManager;
05 import java.sql.ResultSet;
06 import java.sql.SQLException;
07 import java.sql.Statement;
08
09 public class IncluirBanco {
10 static Connection conexao = null;
11
12 static ResultSet resultado = null;
13
14 static Statement instrucao = null;
15
16 static String caminho = "jdbc:mysql://127.0.0.1:3306/java";
17
18 public static void main(String arg[]) throws Exception {
19 try {
20
21 String incluir = "novo nome";
22
23 System.out.println("1. registra o driver");
24 DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
25
26 System.out.println("2. ");
27 conexao = DriverManager.getConnection(caminho, "teste", "123");
28 instrucao = conexao.createStatement();
29
30 System.out.println("3. executa query");
31 String sql = "INSERT INTO cliente ( nome ) VALUES ('" + incluir
32 + "')";
33
34 instrucao = conexao.createStatement();
35 System.out.println("3. executa query");
36 instrucao.executeUpdate(sql);
37
38 } catch (SQLException E) {
39 System.out.println("---------------------------------");
40 System.out.println("Erro ao acessar o banco de dados");
41 System.out.println("SQLException: " + E.getMessage());
42 System.out.println("SQLState: " + E.getSQLState());
43 System.out.println("VendorError: " + E.getErrorCode());
44 System.out.println("---------------------------------");
45 E.printStackTrace();
46 }
47
48 if (resultado != null) {
49 resultado.close();
50 }
51
52 if (instrucao != null) {
53 instrucao.close();
54 }
55
56 if (conexao != null) {
57 conexao.close();
58 }
59 } // main
60 } // classe
LerBanco.java
01 package br.com.thz.samples.db;
02
03 import java.sql.Connection;
04 import java.sql.DriverManager;
05 import java.sql.ResultSet;
06 import java.sql.SQLException;
07 import java.sql.Statement;
08
09 public class LerBanco {
10 static Connection conexao = null;
11
12 static ResultSet resultado = null;
13
14 static Statement instrucao = null;
15
16 static String caminho = "jdbc:mysql://127.0.0.1:3306/java";
17
18 public static void main(String arg[]) throws Exception {
19 try {
20 System.out.println("1. registra o driver");
21 DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
22
23 System.out.println("2. ");
24 conexao = DriverManager.getConnection(caminho, "teste", "123");
25 instrucao = conexao.createStatement();
26
27 System.out.println("3. executa query");
28 String sql = "SELECT * FROM cliente ORDER BY nome";
29 resultado = instrucao.executeQuery(sql);
30
31 String nome;
32
33 while (resultado.next()) {
34 nome = resultado.getString("nome");
35 System.out.println(nome);
36 }
37
38 } catch (SQLException E) {
39 System.out.println("---------------------------------");
40 System.out.println("Erro ao acessar o banco de dados");
41 System.out.println("SQLException: " + E.getMessage());
42 System.out.println("SQLState: " + E.getSQLState());
43 System.out.println("VendorError: " + E.getErrorCode());
44 System.out.println("---------------------------------");
45 E.printStackTrace();
46 }
47
48 if (resultado != null) {
49 resultado.close();
50 }
51
52 if (instrucao != null) {
53 instrucao.close();
54 }
55
56 if (conexao != null) {
57 conexao.close();
58 }
59 } // main
60 } // classe
