[关闭]
@SovietPower 2022-04-26T04:14:59.000000Z 字数 16278 阅读 767

数据库系统 实验4 JDBC

DB



实验

首先运行之前创建的openGauss容器。

连接JDBC

  1. import static java.lang.System.out;
  2. // JDBC
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. public class jdbcConnect
  6. {
  7. static final String JDBC_DRIVER = "org.postgresql.Driver";
  8. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  9. static final String username = "gxb", password="Gxb11111";
  10. public static void main(String args[])
  11. {
  12. try
  13. {
  14. Class.forName(JDBC_DRIVER);
  15. Connection c = DriverManager.getConnection(DB_URL, username, password);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  19. System.exit(0);
  20. }
  21. out.println("Connect to database gxb successfully!");
  22. }
  23. }
  1. PS F:\Codes\Java> & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'jdbcConnect'
  2. Connect to database gxb successfully!

在数据库中创建表

运行jdbcCreate.java

  1. import static java.lang.System.out;
  2. // JDBC
  3. // import java.sql.*;
  4. import java.sql.Connection;
  5. import java.sql.Statement;
  6. // import java.sql.PreparedStatement;
  7. // import java.sql.ResultSet;
  8. // import java.sql.SQLException;
  9. import java.sql.DriverManager;
  10. public class jdbcCreate
  11. {
  12. static final String JDBC_DRIVER = "org.postgresql.Driver";
  13. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  14. static final String username = "gxb", password="Gxb11111";
  15. public static void main(String args[])
  16. {
  17. Connection c = null;
  18. Statement stmt = null;
  19. try
  20. {
  21. Class.forName(JDBC_DRIVER);
  22. c = DriverManager.getConnection(DB_URL, username, password);
  23. out.println("Connect to database gxb successfully!");
  24. stmt = c.createStatement();
  25. String sql = "CREATE TABLE employee (id INT," +
  26. " name VARCHAR(20) NOT NULL, " +
  27. " age INT NOT NULL, " +
  28. " address VARCHAR(50), " +
  29. " salary REAL, " +
  30. "PRIMARY KEY (id))";
  31. stmt.executeUpdate(sql);
  32. stmt.close();
  33. c.close();
  34. }
  35. catch ( Exception e )
  36. {
  37. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  38. System.exit(0);
  39. }
  40. out.println("Create table company successfully!");
  41. }
  42. }
  1. PS F:\Codes\Java> & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'jdbcCreate'
  2. Connect to database gxb successfully!
  3. Create table company successfully!

向表中插入数据

  1. // JDBC
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. // import java.sql.PreparedStatement;
  5. // import java.sql.ResultSet;
  6. // import java.sql.SQLException;
  7. import java.sql.DriverManager;
  8. public class jdbcInsert
  9. {
  10. static final String JDBC_DRIVER = "org.postgresql.Driver";
  11. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  12. static final String username = "gxb", password="Gxb11111";
  13. public static void main(String args[])
  14. {
  15. Connection c = null;
  16. Statement stmt = null;
  17. try
  18. {
  19. Class.forName(JDBC_DRIVER);
  20. c = DriverManager.getConnection(DB_URL, username, password);
  21. c.setAutoCommit(false);
  22. System.out.println("Connect to database gxb successfully!");
  23. stmt = c.createStatement();
  24. String sql = "INSERT INTO employee VALUES (1, 'Gong', 48, '2075 Kongjiang Road', 20000.00 );";
  25. stmt.executeUpdate(sql);
  26. sql = "INSERT INTO employee VALUES (2, 'Luan', 25, '3663 Zhongshan Road(N)', 15000.00 );";
  27. stmt.executeUpdate(sql);
  28. sql = "INSERT INTO employee VALUES (3, 'Hu', 23, '3663 Zhongshan Road(N)', 15000.00 );";
  29. stmt.executeUpdate(sql);
  30. sql = "INSERT INTO employee VALUES (4, 'Jin', 24, '3663 Zhongshan Road(N)', 15000.00 );";
  31. stmt.executeUpdate(sql);
  32. sql = "INSERT INTO employee VALUES (5, 'Yi', 24, '3663 Zhongshan Road(N)', 15000.00 );";
  33. stmt.executeUpdate(sql);
  34. stmt.close();
  35. c.commit();
  36. c.close();
  37. }
  38. catch (Exception e)
  39. {
  40. System.err.println(e.getClass().getName()+ ": "+ e.getMessage() );
  41. System.exit(0);
  42. }
  43. System.out.println("5 records inserted successfully!");
  44. }
  45. }
  1. PS F:\Codes\Java> f:; cd 'f:\Codes\Java'; & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'jdbcInsert'
  2. Connect to database gxb successfully!
  3. 5 records inserted successfully!

在表中查询数据

  1. // JDBC
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. // import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. // import java.sql.SQLException;
  7. import java.sql.DriverManager;
  8. public class jdbcSelect
  9. {
  10. static final String JDBC_DRIVER = "org.postgresql.Driver";
  11. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  12. static final String username = "gxb", password="Gxb11111";
  13. public static void main(String args[])
  14. {
  15. try
  16. {
  17. Class.forName(JDBC_DRIVER);
  18. Connection c = DriverManager.getConnection(DB_URL, username, password);
  19. c.setAutoCommit(false);
  20. System.out.println("Connect to database gxb successfully!");
  21. Statement stmt = c.createStatement();
  22. ResultSet rs = stmt.executeQuery( "SELECT * FROM employee;" );
  23. while (rs.next())
  24. {
  25. int id = rs.getInt("id");
  26. String name = rs.getString("name");
  27. int age = rs.getInt("age");
  28. String address = rs.getString("address");
  29. float salary = rs.getFloat("salary");
  30. System.out.println( "ID = " + id );
  31. System.out.println( "NAME = " + name );
  32. System.out.println( "AGE = " + age );
  33. System.out.println( "ADDRESS = " + address );
  34. System.out.println( "SALARY = " + salary );
  35. System.out.println();
  36. }
  37. rs.close();
  38. stmt.close();
  39. c.close();
  40. }
  41. catch (Exception e)
  42. {
  43. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  44. System.exit(0);
  45. }
  46. System.out.println("Operation done successfully!");
  47. }
  48. }
  1. PS F:\Codes\Java> f:; cd 'f:\Codes\Java'; & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'jdbcSelect'
  2. Connect to database gxb successfully!
  3. ID = 1
  4. NAME = Gong
  5. AGE = 48
  6. ADDRESS = 2075 Kongjiang Road
  7. SALARY = 20000.0
  8. ID = 2
  9. NAME = Luan
  10. AGE = 25
  11. ADDRESS = 3663 Zhongshan Road(N)
  12. SALARY = 15000.0
  13. ID = 3
  14. NAME = Hu
  15. AGE = 23
  16. ADDRESS = 3663 Zhongshan Road(N)
  17. SALARY = 15000.0
  18. ID = 4
  19. NAME = Jin
  20. AGE = 24
  21. ADDRESS = 3663 Zhongshan Road(N)
  22. SALARY = 15000.0
  23. ID = 5
  24. NAME = Yi
  25. AGE = 24
  26. ADDRESS = 3663 Zhongshan Road(N)
  27. SALARY = 15000.0
  28. Operation done successfully!

更新数据

  1. // JDBC
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. // import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. // import java.sql.SQLException;
  7. import java.sql.DriverManager;
  8. public class jdbcUpdate
  9. {
  10. static final String JDBC_DRIVER = "org.postgresql.Driver";
  11. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  12. static final String username = "gxb", password="Gxb11111";
  13. public static void main(String args[])
  14. {
  15. try
  16. {
  17. Class.forName(JDBC_DRIVER);
  18. Connection c = DriverManager.getConnection(DB_URL, username, password);
  19. c.setAutoCommit(false);
  20. System.out.println("Connect to database gxb successfully!");
  21. Statement stmt = c.createStatement();
  22. String sql = "UPDATE employee set SALARY = 50000.00 where ID=1;";
  23. stmt.executeUpdate(sql);
  24. c.commit();
  25. System.out.println("1 record is updated successfully");
  26. ResultSet rs = stmt.executeQuery( "SELECT * FROM employee;" );
  27. while (rs.next())
  28. {
  29. int id = rs.getInt("id");
  30. String name = rs.getString("name");
  31. int age = rs.getInt("age");
  32. String address = rs.getString("address");
  33. float salary = rs.getFloat("salary");
  34. System.out.println( "ID = " + id );
  35. System.out.println( "NAME = " + name );
  36. System.out.println( "AGE = " + age );
  37. System.out.println( "ADDRESS = " + address );
  38. System.out.println( "SALARY = " + salary );
  39. System.out.println();
  40. }
  41. rs.close();
  42. stmt.close();
  43. c.close();
  44. }
  45. catch (Exception e)
  46. {
  47. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  48. System.exit(0);
  49. }
  50. System.out.println("Operation done successfully!");
  51. }
  52. }
  1. PS F:\Codes\Java> f:; cd 'f:\Codes\Java'; & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'jdbcUpdate'
  2. Connect to database gxb successfully!
  3. 1 record is updated successfully
  4. ID = 2
  5. NAME = Luan
  6. AGE = 25
  7. ADDRESS = 3663 Zhongshan Road(N)
  8. SALARY = 15000.0
  9. ID = 3
  10. NAME = Hu
  11. AGE = 23
  12. ADDRESS = 3663 Zhongshan Road(N)
  13. SALARY = 15000.0
  14. ID = 4
  15. NAME = Jin
  16. AGE = 24
  17. ADDRESS = 3663 Zhongshan Road(N)
  18. SALARY = 15000.0
  19. ID = 5
  20. NAME = Yi
  21. AGE = 24
  22. ADDRESS = 3663 Zhongshan Road(N)
  23. SALARY = 15000.0
  24. ID = 1
  25. NAME = Gong
  26. AGE = 48
  27. ADDRESS = 2075 Kongjiang Road
  28. SALARY = 50000.0
  29. Operation done successfully!

删除数据

  1. // JDBC
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. public class jdbcDelete
  7. {
  8. static final String JDBC_DRIVER = "org.postgresql.Driver";
  9. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  10. static final String username = "gxb", password="Gxb11111";
  11. public static void main(String args[])
  12. {
  13. Connection c;
  14. try
  15. {
  16. Class.forName(JDBC_DRIVER);
  17. c = DriverManager.getConnection(DB_URL, username, password);
  18. c.setAutoCommit(false);
  19. System.out.println("Connect to database gxb successfully!");
  20. Statement stmt = c.createStatement();
  21. String sql = "DELETE from employee where ID=2;";
  22. stmt.executeUpdate(sql);
  23. c.commit();
  24. System.out.println("1 record is deleted successfully!");
  25. ResultSet rs = stmt.executeQuery( "SELECT * FROM employee;" );
  26. while (rs.next())
  27. {
  28. int id = rs.getInt("id");
  29. String name = rs.getString("name");
  30. int age = rs.getInt("age");
  31. String address = rs.getString("address");
  32. float salary = rs.getFloat("salary");
  33. System.out.println( "ID = " + id );
  34. System.out.println( "NAME = " + name );
  35. System.out.println( "AGE = " + age );
  36. System.out.println( "ADDRESS = " + address );
  37. System.out.println( "SALARY = " + salary );
  38. System.out.println();
  39. }
  40. rs.close();
  41. stmt.close();
  42. c.close();
  43. }
  44. catch (Exception e)
  45. {
  46. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  47. System.exit(0);
  48. }
  49. System.out.println("Operation done successfully!");
  50. }
  51. }
  1. PS F:\Codes\Java> f:; cd 'f:\Codes\Java'; & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'jdbcDelete'
  2. Connect to database gxb successfully!
  3. 1 record is deleted successfully!
  4. ID = 3
  5. NAME = Hu
  6. AGE = 23
  7. ADDRESS = 3663 Zhongshan Road(N)
  8. SALARY = 15000.0
  9. ID = 4
  10. NAME = Jin
  11. AGE = 24
  12. ADDRESS = 3663 Zhongshan Road(N)
  13. SALARY = 15000.0
  14. ID = 5
  15. NAME = Yi
  16. AGE = 24
  17. ADDRESS = 3663 Zhongshan Road(N)
  18. SALARY = 15000.0
  19. ID = 1
  20. NAME = Gong
  21. AGE = 48
  22. ADDRESS = 2075 Kongjiang Road
  23. SALARY = 50000.0
  24. Operation done successfully!

使用Prepare语句批量插入及更新

  1. // JDBC
  2. import java.sql.*;
  3. public class DBTest
  4. {
  5. static final String JDBC_DRIVER = "org.postgresql.Driver";
  6. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  7. static final String username = "gxb", password="Gxb11111";
  8. //Connect to database
  9. public static Connection GetConnection(String username, String passwd)
  10. {
  11. Connection conn = null;
  12. try {
  13. Class.forName(JDBC_DRIVER).newInstance();
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. return null;
  17. }
  18. try {
  19. conn = DriverManager.getConnection(DB_URL, username, passwd);
  20. System.out.println("Connect to database successfully!");
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. return null;
  24. }
  25. return conn;
  26. }
  27. //Create table customer
  28. public static void CreateTable(Connection conn)
  29. {
  30. Statement stmt = null;
  31. try
  32. {
  33. stmt = conn.createStatement();
  34. stmt.executeUpdate("CREATE TABLE customer(id INTEGER, name VARCHAR(20), PRIMARY KEY (id));");
  35. System.out.println("Create table customer successfully!");
  36. stmt.close();
  37. }
  38. catch (SQLException e)
  39. {
  40. if (stmt != null) {
  41. try {
  42. stmt.close();
  43. } catch (SQLException e1) {
  44. e1.printStackTrace();
  45. }
  46. }
  47. e.printStackTrace();
  48. }
  49. }
  50. //Execute prepare statement, batch insert
  51. public static void BatchInsertData(Connection conn)
  52. {
  53. PreparedStatement pst = null;
  54. try {
  55. pst = conn.prepareStatement("INSERT INTO customer VALUES (?,?)");
  56. for (int i = 0; i < 10; i++)
  57. {
  58. pst.setInt(1, i);
  59. pst.setString(2, "ECNUer " + i);
  60. pst.addBatch();
  61. }
  62. pst.executeBatch();
  63. System.out.println("Insert 10 record in 1 batch successfully!");
  64. pst.close();
  65. }
  66. catch (SQLException e)
  67. {
  68. if (pst != null)
  69. {
  70. try {
  71. pst.close();
  72. } catch (SQLException e1) {
  73. e1.printStackTrace();
  74. }
  75. }
  76. e.printStackTrace();
  77. }
  78. }
  79. //Update by prepare statement
  80. public static void ExecPreparedSQL(Connection conn)
  81. {
  82. PreparedStatement pstmt = null;
  83. try {
  84. pstmt = conn.prepareStatement("UPDATE customer SET name = ? WHERE id = 1");
  85. pstmt.setString(1, "Gong");
  86. pstmt.executeUpdate();
  87. System.out.println("Update custormer 1's name successfully!");
  88. pstmt.close();
  89. }
  90. catch (SQLException e)
  91. {
  92. if (pstmt != null)
  93. {
  94. try {
  95. pstmt.close();
  96. } catch (SQLException e1) {
  97. e1.printStackTrace();
  98. }
  99. }
  100. e.printStackTrace();
  101. }
  102. }
  103. // Main program
  104. public static void main(String[] args)
  105. {
  106. Connection conn = GetConnection(username, password);
  107. CreateTable(conn);
  108. BatchInsertData(conn);
  109. ExecPreparedSQL(conn);
  110. try {
  111. conn.close();
  112. } catch (SQLException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. }
  1. PS F:\Codes\Java> f:; cd 'f:\Codes\Java'; & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'DBTest'
  2. Connect to database successfully!
  3. Create table customer successfully!
  4. Insert 10 record in 1 batch successfully!
  5. Update custormer 1's name successfully!

习题

  1. import java.util.Scanner;
  2. import static java.lang.System.out;
  3. // JDBC
  4. // import java.sql.*;
  5. import java.sql.Connection;
  6. import java.sql.Statement;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.DriverManager;
  11. // mysql
  12. import com.mysql.cj.exceptions.MysqlErrorNumbers;
  13. // class
  14. public class Exercise_5_12
  15. {
  16. static final String JDBC_DRIVER = "org.postgresql.Driver";
  17. static final String DB_URL = "jdbc:postgresql://localhost:15432/db2022";
  18. static final String username = "gxb", password="Gxb11111";
  19. static Connection conn;
  20. public static void main(String... args)
  21. {
  22. // Connect to DB
  23. // a.
  24. Scanner scanner = new Scanner(System.in);
  25. int ret;
  26. if ((ret=ConnectDB(scanner))!=0)
  27. {
  28. while(ret == MysqlErrorNumbers.ER_ACCESS_DENIED_ERROR)
  29. {
  30. out.println("Invalid Password. Access denied.");
  31. ret = ConnectDB(scanner);
  32. }
  33. if (ret != 0)
  34. {
  35. Fail();
  36. return;
  37. }
  38. }
  39. // b.
  40. if (!TaskB(scanner))
  41. {
  42. Fail();
  43. return;
  44. }
  45. // c.
  46. int id = TaskC(scanner);
  47. if (id == 0)
  48. {
  49. Fail();
  50. return;
  51. }
  52. // d.
  53. TaskD(id);
  54. // Finish and close conn and stmt
  55. CloseConnection(conn);
  56. out.println("\nfinish!");
  57. }
  58. static void Fail()
  59. {
  60. out.println("Operation failed. Please check your config.");
  61. }
  62. static boolean TaskB(Scanner scanner)
  63. {
  64. boolean done = false;
  65. ResultSet rs = null;
  66. PreparedStatement pStmt = null;
  67. out.print("\nB: type the name: ");
  68. while(true)
  69. {
  70. String query = scanner.next();
  71. try
  72. {
  73. pStmt = conn.prepareStatement(
  74. "SELECT id, name FROM instructor "+
  75. "WHERE lower(name) like ?"
  76. );
  77. pStmt.setString(1, "%"+query+"%");
  78. rs = pStmt.executeQuery();
  79. while(rs.next())
  80. {
  81. done = true;
  82. out.printf("%d %s\n", rs.getInt(1), rs.getString(2));
  83. }
  84. } catch (SQLException se) {
  85. se.printStackTrace();
  86. return false;
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. return false;
  90. }
  91. // out.println(pStmt.toString());
  92. if (done) break;
  93. out.println("No such ID found.");
  94. }
  95. CloseResultSet(rs);
  96. ClosePreparedStatement(pStmt);
  97. return true;
  98. }
  99. static int TaskC(Scanner scanner)
  100. {
  101. int ret = 0;
  102. ResultSet rs = null;
  103. PreparedStatement pStmt = null;
  104. out.print("\nC: type the ID: ");
  105. int query = scanner.nextInt();
  106. if (query<0 || query>99999)
  107. {
  108. out.println("Invalid id.");
  109. return 0;
  110. }
  111. try
  112. {
  113. pStmt = conn.prepareStatement(
  114. "SELECT id, name FROM instructor "+
  115. "WHERE id=?"
  116. );
  117. pStmt.setInt(1, query);
  118. rs = pStmt.executeQuery();
  119. if (rs.next())
  120. {
  121. ret = rs.getInt(1);
  122. out.printf("ID Found:\n%d %s\n", rs.getInt(1), rs.getString(2));
  123. }
  124. else
  125. out.println("No such name found. Program exiting.");
  126. } catch (SQLException se) {
  127. se.printStackTrace();
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. CloseResultSet(rs);
  132. ClosePreparedStatement(pStmt);
  133. return ret;
  134. }
  135. static void TaskD(int id)
  136. {
  137. boolean ret = false;
  138. ResultSet rs = null;
  139. PreparedStatement pStmt = null;
  140. out.printf("\nD: checking ID: %d\n", id);
  141. try
  142. {
  143. pStmt = conn.prepareStatement(
  144. "WITH temp(course_id, sec_id, semester, year, cnt) AS "+
  145. "(SELECT course_id, sec_id, semester, year, count(*) "+
  146. "FROM teaches inner join takes using(course_id, sec_id, semester, year) "+
  147. "WHERE teaches.id=? "+
  148. "GROUP BY course_id, sec_id, semester, year) "+
  149. "SELECT dept_name, course_id, title, sec_id, semester, year, cnt "+
  150. "FROM course join temp using(course_id) "+
  151. "ORDER BY dept_name, course_id, year, semester"
  152. );
  153. pStmt.setInt(1, id);
  154. rs = pStmt.executeQuery();
  155. while (rs.next())
  156. {
  157. ret = true;
  158. out.printf("%s %s %s %d %s %d %d\n", rs.getString(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getString(5), rs.getInt(6), rs.getInt(7));
  159. }
  160. } catch (SQLException se) {
  161. se.printStackTrace();
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. }
  165. CloseResultSet(rs);
  166. ClosePreparedStatement(pStmt);
  167. if (!ret)
  168. out.println("He/She hasn't taught any course yet.");
  169. }
  170. static int ConnectDB(Scanner scanner)
  171. {
  172. out.println("Input your username and password:");
  173. // String username = "root";
  174. // String password = "GXB";
  175. String username = scanner.next();
  176. String password = scanner.next();
  177. try
  178. {
  179. Class.forName(JDBC_DRIVER);
  180. conn = DriverManager.getConnection(DB_URL, username, password);
  181. }
  182. catch(SQLException se)
  183. {
  184. if (se.getErrorCode() != MysqlErrorNumbers.ER_ACCESS_DENIED_ERROR)
  185. se.printStackTrace();
  186. return se.getErrorCode();
  187. }
  188. catch(Exception e)
  189. {
  190. e.printStackTrace();
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. static void CloseConnection(Connection conn)
  196. {
  197. try {
  198. if(conn!=null) conn.close();
  199. } catch(SQLException se) {
  200. se.printStackTrace();
  201. }
  202. }
  203. static void CloseStatement(Statement stmt)
  204. {
  205. try {
  206. if(stmt!=null) stmt.close();
  207. } catch(SQLException se) {
  208. se.printStackTrace();
  209. }
  210. }
  211. static void ClosePreparedStatement(PreparedStatement pStmt)
  212. {
  213. try {
  214. if(pStmt!=null) pStmt.close();
  215. } catch(SQLException se) {
  216. se.printStackTrace();
  217. }
  218. }
  219. static void CloseResultSet(ResultSet rs)
  220. {
  221. try {
  222. if(rs!=null) rs.close();
  223. } catch(SQLException se) {
  224. se.printStackTrace();
  225. }
  226. }
  227. }
  1. PS F:\Codes\Java> f:; cd 'f:\Codes\Java'; & 'F:\Programs\Java\jdk1.8.0_321\bin\java.exe' '-cp' 'C:\Users\LENOVO\AppData\Local\Temp\cp_2ui26sim2oqork6q7n4daqovk.jar' 'Exercise_5_12'
  2. Input your username and password:
  3. gxb
  4. Gxb11111
  5. B: type the name: o
  6. 63395 McKinnon
  7. 4233 Luo
  8. 50885 Konstantinides
  9. 80759 Queiroz
  10. 97302 Bertolino
  11. 74420 Voronina
  12. 35579 Soisalon-Soininen
  13. 31955 Moreira
  14. 6569 Mingoz
  15. 90643 Choll
  16. 42782 Vicentino
  17. 22591 DAgostino
  18. 52647 Bancilhon
  19. 36897 Morris
  20. 3199 Gustafsson
  21. 34175 Bondi
  22. 33351 Bourrier
  23. 43779 Romero
  24. 77346 Mahmoud
  25. 28400 Atanassov
  26. C: type the ID: 28400
  27. ID Found:
  28. 28400 Atanassov
  29. D: checking ID: 28400
  30. Statistics 603 Care and Feeding of Cats 1 Fall 2003 306
  31. Statistics 604 UNIX System Programmming 1 Spring 2009 300
  32. finish!
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注