Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. public static void main(String args[]) {
  2. Connection conn = null;
  3. PreparedStatement prsment = null;
  4. ResultSet res = null;
  5.  
  6. try {
  7.  
  8.  
  9.  
  10. String usname = "devdbuser";
  11. String usPassword = "postgres";
  12. String host = "jdbc:postgresql://localhost:5432/devdb";
  13. String drayver = "org.postgresql.Driver";
  14. Class.forName(drayver);
  15. conn = DriverManager.getConnection(host, usname, usPassword);
  16. prsment = conn.prepareStatement("SELECT FROM Users");
  17. res = prsment.executeQuery();
  18. while (res.next()){
  19. System.out.print(res.getString(2) + " ");
  20. System.out.print(res.getString(3) + " ");
  21. System.out.print(res.getString(4) + " ");
  22. System.out.print(res.getString(5) + " ");
  23. System.out.print(res.getString(6) + " ");
  24. System.out.print(res.getInt(7));
  25.  
  26. }
  27.  
  28.  
  29.  
  30.  
  31. /* while (res.next()){
  32. int userID = res.getInt("userId");
  33. String userN = res.getString("userName");
  34. String userF = res.getString("userFam");
  35. String userLog = res.getString("userLogin");
  36. String userPsw = res.getString("userPass");
  37. String userPos = res.getString("userEmail");
  38. String userEm = res.getString("userEmail");
  39. int userVozrast = res.getInt("userAge");
  40. System.out.println(userID + " " + userN + " "+ userF + " " + userLog + " " + userPsw + " " + userPos + " " + userEm + " " + userVozrast);
  41.  
  42. }*/
  43.  
  44.  
  45. } catch (Exception n) {
  46. System.out.println("Другая ошибка, проверьте свой код");
  47. }
  48.  
  49. catch (ClassNotFoundException e) {
  50. System.out.println("Проверьте драйвера, правильно ли указали ");
  51. } catch (SQLException e) {
  52. System.out.println("Не удалось подключение к БД");
  53. }
  54. finally{ try{
  55. if (res!=null){
  56. res.close();
  57. }
  58. if (prsment!=null){
  59. prsment.close();
  60. }
  61. if (conn!=null){
  62. conn.close();
  63. }
  64.  
  65. } catch (SQLException e) {
  66. System.out.println("Razyedinenie ne udalos, ili Oshibka pri SQL zaprosa");
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement