Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pstmts;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class PStmts {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://10.129.1.62/Aula";
- String user = "lab";
- String pass = "lab";
- Connection con = DriverManager.getConnection(url, user, pass);
- String sql = "SELECT * FROM Alunos WHERE matricula = ?";
- String sql2 = "UPDATE Alunos SET nome = ? WHERE matricula = ?";
- PreparedStatement cons = con.prepareStatement(sql);
- PreparedStatement cons2 = con.prepareCall(sql2);
- Scanner in = new Scanner(System.in);
- int opcao = 1;
- while (opcao != 0) {
- System.out.print("Opcao: ");
- opcao = in.nextInt();
- if (opcao == 1) {
- System.out.print("Digite a matricula: ");
- int matricula = in.nextInt();
- cons.setInt(1, matricula);
- ResultSet rs = cons.executeQuery();
- while (rs.next()) {
- System.out.println("Nome: " + rs.getString("nome"));
- }
- } else if (opcao == 2) {
- System.out.print("Nome: ");
- String nome = in.next();
- System.out.print("Matricula: ");
- int matricula = in.nextInt();
- cons2.setString(1, nome);
- cons2.setInt(2, matricula);
- cons2.executeUpdate();
- }
- }
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(PStmts.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(PStmts.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement