Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.08 KB | None | 0 0
  1. package BTSinhVien;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class Execute {
  10.    
  11.     public static Connection getConn () {
  12.         try {
  13.             Class.forName("com.mysql.jdbc.Driver");
  14.             String dbUserName = "root";
  15.             String dbPassword = "";
  16.             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8306/NhanVienDB?useUnicode=yes&characterEncoding=UTF-8", dbUserName, dbPassword);
  17.             return conn;
  18.         }
  19.         catch (Exception e) {
  20.             System.out.println ("Lỗi! " + e.getMessage() + "\n---");
  21.             return null;
  22.         }
  23.     }
  24.     public static void dsLop (Connection conn) {
  25.         try {
  26.             Statement stmt = conn.createStatement();
  27.             String sql = "SELECT * FROM Lop";
  28.             ResultSet rs = stmt.executeQuery(sql);
  29.             while (rs.next()) {
  30.                 String malop = rs.getString("MaLop");
  31.                 String tenlop = rs.getString("TenLop");
  32.                 System.out.println ("Mã lop: " + malop + "; Tên Lop: " + tenlop);
  33.             }
  34.         }
  35.         catch (Exception e) {
  36.             System.out.println (e.getMessage());
  37.         }
  38.     }
  39.     public static void dsSinhVien (Connection conn) {
  40.         try {
  41.             Statement stmt = conn.createStatement();
  42.             String sql = "SELECT * FROM SinhVien";
  43.             ResultSet rs = stmt.executeQuery(sql);
  44.             while (rs.next()) {
  45.                 String manv = rs.getString("MaSV");
  46.                 String hoten = rs.getString("TenSV");
  47.                 String ngaysinh = rs.getString("NgaySinh");
  48.                 String gioitinh = rs.getString("GioiTinh");
  49.                 System.out.println (manv + "; Tên: " + hoten + "; Ngày sinh: " + ngaysinh + "; Giới tính: " + gioitinh);
  50.             }
  51.         }
  52.         catch (Exception e) {
  53.             System.out.println (e.getMessage());
  54.         }
  55.     }
  56.     public static void nhapLop (Connection conn) {
  57.         Scanner scan = new Scanner (System.in);
  58.         while (true) {
  59.             System.out.println("---\nTên lớp: ");
  60.             String tenlop = scan.nextLine();
  61.             try {
  62.                 Statement stmt = conn.createStatement();
  63.                 String sql = String.format("INSERT INTO Lop (TenLop) VALUES ('%s')", tenlop);
  64.                 int n = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
  65.                 if (n > 0) {
  66.                     System.out.println ("Thêm thành công!");
  67.                     ResultSet rs = stmt.getGeneratedKeys();
  68.                     if (rs.next())
  69.                         System.out.println ("Mã lớp mới có STT là: " + rs.getInt(1));
  70.                 }
  71.                 else {
  72.                     System.out.println ("Lỗi!");
  73.                 }
  74.             }
  75.             catch (Exception e) {
  76.                 System.out.println (e.getMessage());
  77.             }
  78.             finally {
  79.                 System.out.println ("Tiếp tục? (y/n): ");
  80.                 String out = scan.nextLine();
  81.                 if (!out.equals("y")) {
  82.                     break;
  83.                 }
  84.             }
  85.         }
  86.        
  87.     }
  88.     public static void nhapSinhVien (Connection conn) {
  89.         Scanner scan = new Scanner (System.in);
  90.         while (true) {
  91.             System.out.println("---\nMã sinh viên: ");
  92.             String manv = scan.nextLine();
  93.             System.out.println("Tên sinh viên: ");
  94.             String tennv = scan.nextLine();
  95.             System.out.println("Ngày sinh: ");
  96.             String ngaysinh = scan.nextLine();
  97.             System.out.println("Giới tính: ");
  98.             String gioitinh = scan.nextLine();
  99.             System.out.println("Mã lớp: ");
  100.             String malop = scan.nextLine();
  101.             try {
  102.                 Statement stmt = conn.createStatement();
  103.                 String sql = String.format("INSERT INTO SinhVien VALUES ('%s', '%s', '%s', '%s', '%s')", manv, tennv, ngaysinh, gioitinh, malop);
  104.                 int n = stmt.executeUpdate(sql);
  105.                 if (n==1) {
  106.                     System.out.println ("Thêm thành công!");
  107.                 }
  108.                 else {
  109.                     System.out.println ("Lỗi!");
  110.                 }
  111.             }
  112.             catch (Exception e) {
  113.                 System.out.println (e.getMessage());
  114.             }
  115.             finally {
  116.                 System.out.println ("Tiếp tục? (y/n): ");
  117.                 String out = scan.nextLine();
  118.                 if (!out.equals("y")) {
  119.                     break;
  120.                 }
  121.             }
  122.         }
  123.        
  124.     }
  125.     public static void capNhatSVLop (Connection conn) {
  126.         Scanner scan = new Scanner (System.in);
  127.         while (true) {
  128.             System.out.println("---\nMã sinh viên cần cập nhật lớp: ");
  129.             String masv = scan.nextLine();
  130.             System.out.println("Mã lớp mới: ");
  131.             int malop = Integer.parseInt(scan.nextLine());
  132.             try {
  133.                 Statement stmt = conn.createStatement();
  134.                 String sql = String.format("UPDATE SinhVien SET MaLop = %d WHERE MaSV = '%s'", malop, masv);
  135.                 int n = stmt.executeUpdate(sql);
  136.                 if (n>0) {
  137.                     System.out.println ("Cập nhật thành công!");
  138.                 }
  139.                 else {
  140.                     System.out.println ("Lỗi!");
  141.                 }
  142.             }
  143.             catch (Exception e) {
  144.                 System.out.println (e.getMessage());
  145.             }
  146.             finally {
  147.                 System.out.println ("Tiếp tục? (y/n): ");
  148.                 String out = scan.nextLine();
  149.                 if (!out.equals("y")) {
  150.                     break;
  151.                 }
  152.             }
  153.         }
  154.     }
  155.     public static void xoaSVtheoMaSV (Connection conn) {
  156.         Scanner scan = new Scanner (System.in);
  157.         while (true) {
  158.             System.out.println("---\nMã sinh viên cần xóa: ");
  159.             String masv = scan.nextLine();
  160.             try {
  161.                 Statement stmt = conn.createStatement();
  162.                 String sql = String.format("DELETE FROM SinhVien WHERE MaSV = '%s'", masv);
  163.                 int n = stmt.executeUpdate(sql);
  164.                 if (n>0) {
  165.                     System.out.println ("Xóa thành công!");
  166.                 }
  167.                 else {
  168.                     System.out.println ("Lỗi!");
  169.                 }
  170.             }
  171.             catch (Exception e) {
  172.                 System.out.println (e.getMessage());
  173.             }
  174.             finally {
  175.                 System.out.println ("Tiếp tục? (y/n): ");
  176.                 String out = scan.nextLine();
  177.                 if (!out.equals("y")) {
  178.                     break;
  179.                 }
  180.             }
  181.         }
  182.     }
  183.     public static void dsSinhVienTheoLop (Connection conn) {
  184.         Scanner scan = new Scanner (System.in);
  185.         while (true) {
  186.             System.out.println("---\nMã lớp: ");
  187.             String malop = scan.nextLine();
  188.             try {
  189.                 Statement stmt = conn.createStatement();
  190.                 String sql = String.format("SELECT * FROM SinhVien WHERE MaLop = '%s'", malop);
  191.                 ResultSet rs = stmt.executeQuery(sql);
  192.                 while (rs.next()) {
  193.                     String tensv = rs.getString("TenSV");
  194.                     System.out.println ("Ten SV: " + tensv);
  195.                 }
  196.             }
  197.             catch (Exception e) {
  198.                 System.out.println (e.getMessage());
  199.             }
  200.             finally {
  201.                 System.out.println ("Tiếp tục? (y/n): ");
  202.                 String out = scan.nextLine();
  203.                 if (!out.equals("y")) {
  204.                     break;
  205.                 }
  206.             }
  207.         }
  208.     }
  209.     public static void main(String[] args) {
  210.         // TODO code application logic here
  211.         Scanner scan = new Scanner (System.in);
  212.         Connection conn = getConn();
  213.         dsLop (conn);
  214.         dsSinhVien (conn);
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement