Guest User

Untitled

a guest
Dec 5th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package sql2008;
  6. import java.sql.*;
  7.  
  8. /**
  9. *
  10. * @author Sony
  11. */
  12. public class Sql2008 {
  13.  
  14. /**
  15. * @param args the command line arguments
  16. */
  17. public static void main(String[] args) {
  18. Connection con = null;
  19. try
  20. {
  21. String userName="sa";
  22. String password="123456";
  23. String url="jdbc:sqlserver://localhost:1433;Instance=SQL2008;databaseName=demo;";
  24. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  25. con = java.sql.DriverManager.getConnection(url,userName,password);
  26. System.out.println("Da ket noi CSDL");
  27. java.sql.Statement st=con.createStatement();
  28. String sqlString ="";
  29. sqlString="UPDATE sinhvien SET ten='Abc' WHERE masv='01'";
  30. st.executeUpdate(sqlString);
  31. sqlString="INSERT INTO sinhvien VALUES(05,'manh','java')";
  32. st.executeUpdate(sqlString);
  33. sqlString="SELECT * FROM sinhvien";
  34. ResultSet rs=st.executeQuery(sqlString);
  35. while(rs.next())
  36. {
  37. System.out.print("masv:"+rs.getString(1)+" ");
  38. System.out.print("hodem:"+rs.getString(2)+" ");
  39. System.out.println();
  40. }
  41. }
  42. catch (Exception e)
  43. {
  44. System.err.println("khong ket noi duoc :"+e.getMessage());
  45. }
  46. finally
  47. {
  48. if(con!=null)
  49. {
  50. try
  51. {
  52. con.close();
  53. System.out.println("Dong ket noi");
  54. }
  55. catch(Exception e){}
  56. }
  57. }
  58. }
  59. // TODO code application logic here
  60. }
Add Comment
Please, Sign In to add comment