Advertisement
ModestGenius

Work with database

Feb 22nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.sql.*;
  4.  
  5. public class JDBC {
  6.  
  7. public static void main(String[] argv) throws SQLException {
  8.  
  9. System.out.println("-------- PostgreSQL "
  10. + "JDBC Connection Testing ------------");
  11.  
  12. try {
  13.  
  14. Class.forName("org.postgresql.Driver");
  15.  
  16. } catch (ClassNotFoundException e) {
  17.  
  18. System.out.println("Where is your PostgreSQL JDBC Driver? "
  19. + "Include in your library path!");
  20. e.printStackTrace();
  21. return;
  22.  
  23. }
  24.  
  25. System.out.println("PostgreSQL JDBC Driver Registered!");
  26.  
  27. Connection connection = null;
  28.  
  29. try {
  30.  
  31. connection = DriverManager.getConnection(
  32. "jdbc:postgresql://localhost:5432/Schedule","postgres","postgres");
  33.  
  34. }
  35.  
  36. catch (SQLException e) {
  37.  
  38. System.out.println("Connection Failed! Check output console");
  39. e.printStackTrace();
  40. return;
  41.  
  42. }
  43.  
  44. Statement st = connection.createStatement();
  45. ResultSet rs = st.executeQuery("select * from Факультет" );
  46. while(rs.next()){
  47. System.out.println(rs.getInt("PK_Факультет") + " " + rs.getString("Наименование"));//Последовательно для каждой строки выводим значение из колонки ColumnName
  48. }
  49. if (connection != null) {
  50. try {
  51. connection.close();
  52. } catch (SQLException ex) {
  53. ex.printStackTrace();
  54. }
  55. System.out.println("You made it, take control your database now!");
  56. } else {
  57. System.out.println("Failed to make connection!");
  58. }
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement