Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Prototipo para acessar banco de dados pelo Java(Eclipse+Mysql)
- //Aluno : Alan Cesar.
- //Projeto: Unicarioca.
- //Softwares: Eclipse + MySQL.
- //RJ 15 de Março 2017.
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import javax.swing.JOptionPane;
- public class Projeto01 {
- private Connection conexao=null;
- private Statement estrutura=null;
- private ResultSet resposta=null;
- public void coneCtar(){
- String servidor="jdbc:mysql://localhost:3306/agenda";
- String usuario="root";
- String senha=null;
- String drive="com.mysql.jdbc.Driver";
- try{
- Class.forName(drive);
- this.conexao=DriverManager.getConnection(servidor,usuario,senha);
- this.estrutura=this.conexao.createStatement();
- }catch(Exception e){
- System.out.print("Erro : "+e.getMessage());}
- }
- public boolean coneCtado(){
- if(this.conexao != null){
- return true;
- }else{
- return false;
- }
- }
- public void inserirDados(String Id,String Nome ,String tel){
- try{
- String consulta="Insert into contato values('"+lerId(Id)+"','"+lerNome(Nome)+"','"+lerTel(tel)+"') ";
- this.estrutura.executeUpdate(consulta);
- }catch(Exception e){
- System.out.print("Erro : "+e.getMessage());}
- }
- public void listarDados(){
- try{
- String consulta = "Select * from contato";
- this.resposta=this.estrutura.executeQuery(consulta);
- while(this.resposta.next()){
- System.out.println("Id "+this.resposta.getString("Id")+"Nome "+this.resposta.getString("Nome")+"Tel "+this.resposta.getString("tel"));
- }
- }catch(Exception e){
- System.out.print("Erro : "+e.getMessage());
- }
- }
- public void fecharBanco(){
- try{
- this.conexao.close();
- System.out.print("<<<Acesso encerrado com sucesso>>>");
- }catch(Exception e){
- System.out.print("Erro : "+e.getMessage());
- }
- }
- public int lerId(String id){
- String aS=JOptionPane.showInputDialog("Id : ");
- int a=Integer.parseInt(aS);
- return a;
- }
- public String lerNome(String nome){
- String b=JOptionPane.showInputDialog("Nome : ");
- return b;
- }
- public int lerTel(String tel){
- String cS=JOptionPane.showInputDialog("tel : ");
- int c=Integer.parseInt(cS);
- return c;
- }
- }
- }catch(Exception e){
- System.out.println("Erro : "+e.getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement