Advertisement
Guest User

Db.java

a guest
May 16th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package cirobd;
  7.  
  8. import java.sql.*;
  9. import javax.swing.JOptionPane;
  10. import com.mysql.jdbc.Driver;
  11.  
  12.  
  13. public class DB {
  14.  
  15. String maq = "localhost";
  16. String porta = "3306";
  17. String user = "root";
  18. String senha = "root";
  19. String banco = "dbagenda";
  20.  
  21. String driver = "com.mysql.jdbc.Driver";
  22. private Statement st = null;
  23. private Connection conn = null;
  24.  
  25. public DB() throws Exception {
  26. Class.forName(driver);
  27. conn = DriverManager.getConnection("jdbc:mysql://" + maq + ":" + porta + "/" + banco, user, senha);
  28.  
  29. st = conn.createStatement();
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. public void inserir( String Nome, String Cidade) throws Exception {
  38. String comandoSql = "insert into tbagenda(Nome,Cidade) values(" + "\"" + Nome + "\",\"" + Cidade + "\")";
  39.  
  40. st.executeUpdate(comandoSql);
  41. }
  42.  
  43.  
  44. public void listar()throws Exception{
  45. String comandoSql="select * from tbagenda";
  46.  
  47. String str="";
  48.  
  49. ResultSet resp=st.executeQuery(comandoSql);
  50. while(resp.next()){
  51.  
  52.  
  53. System.out.println(resp.getString("codigo")+"\t");
  54. System.out.println(resp.getString("nome")+"\t");
  55. System.out.println(resp.getString("cidade")+"\t");
  56. str+=resp.getString("codigo")+"";
  57. str+=resp.getString("nome")+"";
  58. str+=resp.getString("cidade")+"";
  59. JOptionPane.showMessageDialog(null,"Resultado...\n"+str);
  60.  
  61. }
  62.  
  63. }
  64.  
  65. public void apagar(String Codigo)throws Exception{
  66. //deletando dados do banco de dados where onde codigo for =codigo//
  67.  
  68. String sql="delete * from tbagenda where Codigo"+Codigo;
  69. st.executeUpdate(sql);
  70.  
  71. }
  72. // a linha abaixo close fecha a consulta com o banco de dados//
  73. public void close() throws Exception{
  74. conn.close();
  75. st.close();
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement