Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.swalahamani.ts.test;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author swalahamani
- */
- public class JDBC_Ex {
- private final String DB_URL = "jdbc:postgresql://localhost:5432/DB_NAME";
- private final String DB_USR = "postgres";
- private final String DB_PAS = "password";
- /*
- sql qyert -> SELECT * FROM DAILY_EXPENSE WHERE AMOUNT = 45;
- showDBTable("DAILY_EXPENSE", "AMOUNT", "45");
- */
- public void showDBTable(String tableName, String colName, String value) {
- Connection con;
- Statement smt;
- ResultSet rs;
- try {
- Class.forName("org.postgresql.Driver");
- con = DriverManager.getConnection(DB_URL, DB_USR, DB_PAS);
- smt = con.createStatement();
- rs = smt.executeQuery("SELECT * FROM "+tableName+" WHERE "+colName+" = "+value);
- while(rs.next()) {
- System.out.println(rs.getString("DATEE"));
- System.out.println(rs.getString("NAME"));
- System.out.println(rs.getString("DESCRIPTION"));
- System.out.println(rs.getString("AMOUNT"));
- }
- rs.close();
- smt.close();
- con.close();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(JDBC_Ex.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(JDBC_Ex.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Add Comment
Please, Sign In to add comment