Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. package laba1;
  2.  
  3. import java.sql.*;
  4. import java.util.Properties;
  5.  
  6. public class Main {
  7.  
  8. private static final String CONN_URL = "jdbc:mysql://localhost:3306/dblab";
  9. private static final String USERNAME = "root";
  10. private static final String PASSWORD = "1111";
  11.  
  12. public static void main(String[] args) throws ClassNotFoundException {
  13. Class.forName("com.mysql.jdbc.Driver");
  14. Properties props = new Properties();
  15. props.setProperty("user", USERNAME);
  16. props.setProperty("password", PASSWORD);
  17. Connection conn = null;
  18. Statement stmt;
  19. try {
  20. conn = DriverManager.getConnection(CONN_URL, props);
  21. stmt = conn.createStatement();
  22. System.out.println("Connected to database");
  23. String sql = "alter table second add gruppa7 text";
  24. stmt.executeUpdate(sql);
  25. sql = "update second set gruppa7='ka-67' where id='1'";
  26. stmt.executeUpdate(sql);
  27. sql = "update second set gruppa7='ka-24' where id='2'";
  28. stmt.executeUpdate(sql);
  29. sql = "update second set gruppa7='ka-53' where id='3'";
  30. stmt.executeUpdate(sql);
  31. sql = "select * from second";
  32. ResultSet rs = stmt.executeQuery(sql);
  33. //STEP 5: Extract data from result set
  34. while(rs.next()){
  35. System.out.println(rs.getInt("second.id") + ", " + rs.getString("second.surname") + ", "
  36. + rs.getString("second.gruppa7"));
  37. }
  38. rs.close();
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. } finally {
  42. if (conn != null) {
  43. try {
  44. conn.close();
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement