Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 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 server.jdbc.sinhvien;
  7. import java.io.DataInputStream;
  8. import java.io.DataOutputStream;
  9. import java.io.IOException;
  10. import java.net.ServerSocket;
  11. import java.net.Socket;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19. /**
  20. *
  21. * @author Phungg
  22. */
  23. public class ServerJDBCSinhVien {
  24.  
  25. /**
  26. * @param args the command line arguments
  27. */
  28. public static class LoginUser extends Thread
  29. {
  30.  
  31. Socket s;
  32. LoginUser(Socket sv)
  33. {
  34. this.s=sv;
  35. }
  36. public void run()
  37. {
  38. Connection conn = null;
  39. DataInputStream receive;
  40. DataOutputStream send;
  41. try {
  42. receive = new DataInputStream(s.getInputStream());
  43. send = new DataOutputStream(s.getOutputStream());
  44. try
  45. {
  46. String user = "sa";
  47. String pass = "123456";
  48. String url = "jdbc:sqlserver://localhost:1433;databaseName=QLSV;";
  49. conn = DriverManager.getConnection(url,user,pass);
  50. Statement stm;
  51. ResultSet rs;
  52. ResultSet rs1;
  53. ResultSet rs2;
  54. try
  55. {
  56. while(true)
  57. {
  58. String UserReceive = receive.readUTF();
  59. String PasswordReceive = receive.readUTF();
  60. stm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  61. rs = stm.executeQuery("select * from SinhVien where UserName = '"+UserReceive+"' and Password ='"+PasswordReceive+"'");
  62. if(rs.next())
  63. {
  64. send.writeUTF("Menu: \n 1.Xem Diem \n 2.Nhap Diem \n 3.Thoat");
  65. break;
  66. }
  67. else
  68. send.writeUTF("user hoac password khong dung!");
  69. }
  70. while(true)
  71. {
  72. send.writeUTF("nhap vao lua chon cua ban: ");
  73. int choice = receive.readInt();
  74. if(choice==1)
  75. {
  76. send.writeUTF("Nhap vao ma sinh vien: ");
  77. String mssv = receive.readUTF();
  78. stm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  79. rs1 = stm.executeQuery("select * from ThongTin where MaSV = '"+mssv+"'");
  80. if(rs1.next())
  81. {
  82. send.writeUTF(rs1.getString("MaSV")+" "+rs1.getString("HoTen")+" "+rs1.getString("DiemTB"));
  83. }
  84. else
  85. send.writeUTF("Ma sinh vien khong ton tai!");
  86. }
  87. else
  88. if(choice==2)
  89. {
  90. send.writeUTF("Nhap vao ma sinh vien: ");
  91. String ms = receive.readUTF();
  92. send.writeUTF("Nhap vao ho va ten: ");
  93. String hoten = receive.readUTF();
  94. send.writeUTF("Nhap vao diem trung binh: ");
  95. String diem = receive.readUTF();
  96. stm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  97. rs2 = stm.executeQuery("select MaSV from ThongTin where MaSV = '"+ms+"'");
  98. if(rs2.next())
  99. {
  100. send.writeUTF("ma sinh vien da ton tai. cap nhat that bai!");
  101. }
  102. else
  103. {
  104. send.writeUTF("cap nhat thanh cong");
  105. // them querry tai day !!!
  106. }
  107. }
  108. }
  109. //rs.close();
  110. //stm.close();
  111. }catch(Exception e)
  112. {
  113. e.printStackTrace();
  114. }
  115. }catch (SQLException ex)
  116. {
  117. ex.printStackTrace();
  118. }
  119. finally
  120. {
  121. try {
  122. if (conn != null && !conn.isClosed())
  123. {
  124. conn.close();
  125. }
  126. } catch (SQLException ex)
  127. {
  128. ex.printStackTrace();
  129. }
  130. }
  131. } catch (IOException ex)
  132. {
  133. Logger.getLogger(ServerJDBCSinhVien.class.getName()).log(Level.SEVERE, null, ex);
  134. }
  135. }
  136. }
  137.  
  138. public static synchronized void main (String[] args) throws IOException{
  139. ServerSocket ss = new ServerSocket(1234);
  140. while(true)
  141. {
  142. Socket sv = ss.accept();
  143. LoginUser a = new LoginUser(sv);
  144. a.start();
  145. }
  146. // TODO code application logic here
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement