Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.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 sql;
  7. import java.sql.Connection;
  8. import java.sql.DatabaseMetaData;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13.  
  14. /**
  15. *
  16. * @author Tu Anh
  17. */
  18. public class SQL {
  19.  
  20. /**
  21. * @param args the command line arguments
  22. */
  23. public static void main(String[] args) throws ClassNotFoundException {
  24. // TODO code application logic here
  25. Connection conn = null;
  26. try{
  27. String user="sa";
  28. String pass = "123456";
  29. String url = "jdbc:sqlserver://localhost:1433;databaseName=bangdiem;";
  30. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  31. conn = DriverManager.getConnection(url,user,pass);
  32. if (conn != null) {
  33. DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
  34. System.out.println("Driver name: " + dm.getDriverName());
  35. System.out.println("Driver version: " + dm.getDriverVersion());
  36. System.out.println("Product name: " + dm.getDatabaseProductName());
  37. System.out.println("Product version: " + dm.getDatabaseProductVersion());
  38. }
  39. Statement stm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  40. ResultSet rs = stm.executeQuery("select * from diemthi");
  41. rs.beforeFirst();
  42. while(rs.next()){
  43. System.out.println(rs.getString("tensinhvien")+" "+ rs.getFloat("diemtoan")+ " "+ rs.getFloat("diemly")+" "+rs.getFloat("diemhoa"));
  44. }
  45. rs.close();
  46. stm.close();
  47.  
  48. }
  49. catch(SQLException ex){
  50. ex.printStackTrace();
  51. }finally {
  52. try {
  53. if (conn != null && !conn.isClosed()) {
  54. conn.close();
  55.  
  56. }
  57. } catch (SQLException ex) {
  58. ex.printStackTrace();
  59. }
  60. }
  61.  
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement