Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. package javaapplication2;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class JavaApplication2 {
  10. private final static String DBURL = "jdbc:mysql://localhost:1433";
  11. private final static String DBUSER = "ktos";
  12. private final static String DBPASS = "pass";
  13. private final static String DBDRIVER = "com.mysql.jdbc.Driver";
  14.  
  15. private static Connection connection;
  16. private static Statement statement;
  17.  
  18. public static void main(String[] args) {
  19. try{
  20. Class.forName(DBDRIVER);
  21.  
  22. System.out.println("start");
  23. connection = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
  24. System.out.println("next");
  25.  
  26. statement = connection.createStatement();
  27. String sql = "SELECT id, first, last, age FROM Employees";
  28. ResultSet rs = statement.executeQuery(sql);
  29.  
  30. while(rs.next()){
  31. int id = rs.getInt("id");
  32. int age = rs.getInt("age");
  33. String first = rs.getString("first");
  34. String last = rs.getString("last");
  35.  
  36. System.out.print("ID: " + id);
  37. System.out.print(", Age: " + age);
  38. System.out.print(", First: " + first);
  39. System.out.println(", Last: " + last);
  40. }
  41. //STEP 6: Clean-up environment
  42. rs.close();
  43. statement.close();
  44. connection.close();
  45. }catch(SQLException se){
  46. //Handle errors for JDBC
  47. se.printStackTrace();
  48. }catch(Exception e){
  49. //Handle errors for Class.forName
  50. e.printStackTrace();
  51. }finally{
  52. try{
  53. if(statement!=null) statement.close();
  54. }catch(SQLException se2){
  55. }
  56. try{
  57. if(connection!=null) connection.close();
  58. }catch(SQLException se){
  59. se.printStackTrace();
  60. }
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement