Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package pstmts;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Scanner;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. public class PStmts {
  13.  
  14.     public static void main(String[] args) {
  15.         try {
  16.             Class.forName("com.mysql.jdbc.Driver");
  17.  
  18.             String url = "jdbc:mysql://10.129.1.62/Aula";
  19.             String user = "lab";
  20.             String pass = "lab";
  21.  
  22.             Connection con = DriverManager.getConnection(url, user, pass);
  23.  
  24.             String sql = "SELECT * FROM Alunos WHERE matricula = ?";
  25.             String sql2 = "UPDATE Alunos SET nome = ? WHERE matricula = ?";
  26.             PreparedStatement cons = con.prepareStatement(sql);
  27.             PreparedStatement cons2 = con.prepareCall(sql2);
  28.  
  29.             Scanner in = new Scanner(System.in);
  30.             int opcao = 1;
  31.  
  32.             while (opcao != 0) {
  33.                 System.out.print("Opcao: ");
  34.                 opcao = in.nextInt();
  35.                
  36.                 if (opcao == 1) {
  37.                     System.out.print("Digite a matricula: ");
  38.                     int matricula = in.nextInt();
  39.  
  40.                     cons.setInt(1, matricula);
  41.                     ResultSet rs = cons.executeQuery();
  42.  
  43.                     while (rs.next()) {
  44.                         System.out.println("Nome: " + rs.getString("nome"));
  45.                     }
  46.                 } else if (opcao == 2) {
  47.                     System.out.print("Nome: ");
  48.                     String nome = in.next();
  49.                     System.out.print("Matricula: ");
  50.                     int matricula = in.nextInt();
  51.                    
  52.                     cons2.setString(1, nome);
  53.                     cons2.setInt(2, matricula);
  54.                     cons2.executeUpdate();
  55.                 }
  56.             }
  57.         } catch (ClassNotFoundException ex) {
  58.             Logger.getLogger(PStmts.class.getName()).log(Level.SEVERE, null, ex);
  59.         } catch (SQLException ex) {
  60.             Logger.getLogger(PStmts.class.getName()).log(Level.SEVERE, null, ex);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement