Advertisement
Guest User

Untitled

a guest
May 12th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. private void Conecta(){
  2. // try{
  3. // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  4. // }
  5. // catch (Exception excessao){
  6. // JOptionPane.showMessageDialog(null," SQL Exception "+ excessao.getMessage()+
  7. // " Erro de Carga de Banco de Dados ");
  8. // }
  9. try {
  10. conexao = DriverManager.getConnection("jdbc:mysql://localhost:8080/cursos", "root", "");
  11. System.out.println("Conectado!");
  12. comando = conexao.createStatement();
  13. }
  14. catch (Exception excessao){
  15. JOptionPane.showMessageDialog(null," SQL Exception "+ excessao.getMessage()+
  16. " Erro ao conectar ao Carga de Banco de Dados ");
  17. }
  18.  
  19. }
  20.  
  21. private void InsereDados() {
  22. do{
  23. strCurso = (String)JOptionPane. showInputDialog("Digite o numero do Curso:");
  24. }while(strCurso == null);
  25.  
  26. do{
  27. strNome = (String)JOptionPane. showInputDialog("Digite o nome do Curso:");
  28. }while(strNome == null);
  29.  
  30. do{
  31. strDuracao = (String)JOptionPane. showInputDialog("Digite A Carga Horaria do Curso:");
  32. }while(strDuracao == null);
  33.  
  34. private void Insert(){
  35. if ((strCurso != null)&&(strNome != null)&&(strDuracao != null)) {
  36. try {
  37. PreparedStatement strComandoSQL ;
  38. strComandoSQL = conexao.prepareStatement( "INSERT INTO curso(CDCurso,NMCurso,NRDuracao)" + " VALUES(?,?,?)");
  39. strComandoSQL.setInt(1,Integer.parseInt(strCurso));
  40. strComandoSQL.setString(2,strNome);
  41. strComandoSQL.setInt(3,Integer.parseInt(strDuracao));
  42. int intRegistro = strComandoSQL.executeUpdate();
  43. if (intRegistro != 0){
  44. JOptionPane.showMessageDialog(null,"Registro adicionado !", "Mensagem",JOptionPane.INFORMATION_MESSAGE);
  45. comando.close();
  46. conexao.close();
  47. }
  48. else{
  49. JOptionPane.showMessageDialog(null,"Registro nao adicionado !", "Mensagem",JOptionPane.INFORMATION_MESSAGE);
  50. comando.close();
  51. conexao.close();
  52. }
  53. }
  54. catch (Exception Excecao) {
  55. JOptionPane.showMessageDialog(null,"SQLException: " + Excecao.getMessage(),"Erro: Selecaoo de registro", JOptionPane.INFORMATION_MESSAGE);
  56. }
  57. } else {
  58. JOptionPane.showMessageDialog(null,"Falta Preencher Campo!", "Mensagem",JOptionPane.INFORMATION_MESSAGE);
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement