Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.sql.*;
- public class JDBC {
- public static void main(String[] argv) throws SQLException {
- System.out.println("-------- PostgreSQL "
- + "JDBC Connection Testing ------------");
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Where is your PostgreSQL JDBC Driver? "
- + "Include in your library path!");
- e.printStackTrace();
- return;
- }
- System.out.println("PostgreSQL JDBC Driver Registered!");
- Connection connection = null;
- try {
- connection = DriverManager.getConnection(
- "jdbc:postgresql://localhost:5432/Schedule","postgres","postgres");
- }
- catch (SQLException e) {
- System.out.println("Connection Failed! Check output console");
- e.printStackTrace();
- return;
- }
- Statement st = connection.createStatement();
- ResultSet rs = st.executeQuery("select * from Факультет" );
- while(rs.next()){
- System.out.println(rs.getInt("PK_Факультет") + " " + rs.getString("Наименование"));//Последовательно для каждой строки выводим значение из колонки ColumnName
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- System.out.println("You made it, take control your database now!");
- } else {
- System.out.println("Failed to make connection!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement