Guest User

Untitled

a guest
Apr 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. // JDBC driver name and database URL
  2. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  3. static final String DB_URL = "jdbc:mysql://10.10.61.171:3306/Priem_zayvok";
  4.  
  5. // Database credentials
  6. static final String USER = "GLAZIK";
  7. static final String PASS = "";
  8.  
  9. public static void main(String[] args) {
  10. Connection conn = null;
  11. Statement stmt = null;
  12. try{
  13. //STEP 2: Register JDBC driver
  14. Class.forName("com.mysql.jdbc.Driver");
  15.  
  16. //STEP 3: Open a connection
  17. System.out.println("Connecting to a selected database...");
  18. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  19. System.out.println("Connected database successfully...");
  20.  
  21. //STEP 4: Execute a query
  22. System.out.println("Creating statement...");
  23. stmt = conn.createStatement();
  24.  
  25. String sql = "SELECT * FROM Zayavka";
  26. ResultSet rs = stmt.executeQuery(sql);
  27. //STEP 5: Extract data from result set
  28. while(rs.next()){
  29. //Retrieve by column name
  30. int id = rs.getInt("id");
  31. String age = rs.getString("polzovatel");
  32.  
  33.  
  34. //Display values
  35. System.out.print("id: " + id);
  36. System.out.print(", polzovatel: " + age);
  37.  
  38. }
  39. rs.close();
  40. }catch(SQLException se){
  41. //Handle errors for JDBC
  42. se.printStackTrace();
  43. }catch(Exception e){
  44. //Handle errors for Class.forName
  45. e.printStackTrace();
  46. }finally{
  47. //finally block used to close resources
  48. try{
  49. if(stmt!=null)
  50. conn.close();
  51. }catch(SQLException se){
  52. }// do nothing
  53. try{
  54. if(conn!=null)
  55. conn.close();
  56. }catch(SQLException se){
  57. se.printStackTrace();
  58. }//end finally try
  59. }//end try
  60. System.out.println("Goodbye!");
  61. }
Add Comment
Please, Sign In to add comment