Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package cirobd;
- import java.sql.*;
- import javax.swing.JOptionPane;
- import com.mysql.jdbc.Driver;
- public class DB {
- String maq = "localhost";
- String porta = "3306";
- String user = "root";
- String senha = "ciro1234";
- String banco = "dbagenda";
- String driver = "com.mysql.jdbc.Driver";
- private Statement st = null;
- private Connection conn = null;
- public DB() throws Exception {
- Class.forName(driver);
- conn = DriverManager.getConnection("jdbc:mysql://" + maq + ":" + porta + "/" + banco, user, senha);
- st = conn.createStatement();
- }
- public void inserir(String Codigo, String Nome, String Cidade) throws Exception {
- String comandoSql = "insert into tbagenda(Codigo,Nome,Cidade) values(" + Codigo + "," + "\"" + Nome + "\",\"" + Cidade + "\")";
- st.executeUpdate(comandoSql);
- }
- public void listar()throws Exception{
- String comandoSql="select * from tbagenda";
- String str="";
- ResultSet resp=st.executeQuery(comandoSql);
- while(resp.next()){
- System.out.println(resp.getString("codigo")+"\t");
- System.out.println(resp.getString("nome")+"\t");
- System.out.println(resp.getString("cidade")+"\t");
- str+=resp.getString("codigo")+"";
- str+=resp.getString("nome")+"";
- str+=resp.getString("cidade")+"";
- JOptionPane.showMessageDialog(null,"Resultado...\n"+str);
- }
- }
- public void apagar(String Codigo)throws Exception{
- //deletando dados do banco de dados where onde codigo for =codigo//
- String sql="delete * from tbagenda where Codigo="+Codigo;
- st.executeUpdate(sql);
- }
- // a linha abaixo close fecha a consulta com o banco de dados//
- public void close() throws Exception{
- conn.close();
- st.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement