Advertisement
Guest User

Untitled

a guest
Sep 10th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. import java.sql.* ;
  2. import java.util.Properties;
  3.  
  4. public class BathUniDatabase {
  5.  
  6. public static void main(String[] args){
  7. BathUniDatabase db = new BathUniDatabase();
  8. Connection con;
  9. try {
  10. con = db.getConnection();
  11. db.viewStudentTable(con, "bathuni");
  12. System.out.println("\n---\n");
  13. // *** Edit Student Details ***
  14. //db.editStudentTable(con,"bathuni",1,"Michael");
  15. //db.editStudentTable(con,"bathuni",4,"Louise");
  16. //db.viewStudentTable(con, "bathuni");
  17. //System.out.println("\n---\n");
  18. // *** Enrol a Student ***
  19. //db.enrolStudent(con, "bathuni", "Maria");
  20. db.viewStudentTable(con, "bathuni");
  21. // *** Un-Enrol a Student ***
  22. db.unenrolStudent(con, "bathuni", 16);
  23. db.viewStudentTable(con, "bathuni");
  24. } catch (SQLException e) {
  25. System.err.println(e.toString());
  26. }
  27. }
  28.  
  29. public Connection getConnection()
  30. throws SQLException {
  31.  
  32. Properties connectionProps = new Properties();
  33. connectionProps.put("user", "root");
  34. connectionProps.put("password", "");
  35. Connection conn = DriverManager.getConnection(
  36. "jdbc:mysql://localhost:3306/",
  37. connectionProps);
  38.  
  39. System.out.println("Connected to database");
  40. return conn;
  41. }
  42.  
  43. public void viewStudentTable(Connection con, String dbName) throws SQLException{
  44. Statement stmt = null;
  45. String query = "select * from " + dbName + ".student";
  46. ResultSet rs = null;
  47. try {
  48. stmt = con.createStatement();
  49. rs = stmt.executeQuery(query);
  50. while (rs.next()) {
  51. int studentID = rs.getInt("ID");
  52. String name = rs.getString("studentname");
  53. System.out.println(studentID + "\t" + name);
  54. }
  55. }
  56. catch (SQLException e ) {
  57. System.err.println(e.toString());
  58. }
  59. finally {
  60. if (stmt != null){
  61. stmt.close();
  62. }
  63. }
  64. }
  65.  
  66. public void editStudentTable(Connection con, String dbName, int ID, String name) throws SQLException{
  67. Statement stmt = null;
  68. String query = "update " + dbName + ".student set studentname = \"" + name + "\" where ID = " + ID;
  69. try {
  70. stmt = con.createStatement();
  71. stmt.executeUpdate(query);
  72. }
  73. catch (SQLException e ) {
  74. System.err.println(e.toString());
  75. }
  76. finally {
  77. if (stmt != null){
  78. stmt.close();
  79. }
  80. }
  81. }
  82.  
  83. public void enrolStudent(Connection con, String dbName, String name) throws SQLException{
  84. Statement stmt = null;
  85. String query = "insert into " + dbName + ".student values(NULL,\"" + name + "\")";
  86. try {
  87. stmt = con.createStatement();
  88. stmt.executeUpdate(query);
  89. }
  90. catch (SQLException e ) {
  91. System.err.println(e.toString());
  92. }
  93. finally {
  94. if (stmt != null){
  95. stmt.close();
  96. }
  97. }
  98. }
  99.  
  100. public void unenrolStudent(Connection con, String dbName, int ID) throws SQLException{
  101. Statement stmt = null;
  102. String query = "delete from " + dbName + ".student where ID = " + ID;
  103. try {
  104. stmt = con.createStatement();
  105. stmt.executeUpdate(query);
  106. }
  107. catch (SQLException e ) {
  108. System.err.println(e.toString());
  109. }
  110. finally {
  111. if (stmt != null){
  112. stmt.close();
  113. }
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement