Created
November 3, 2017 06:28
-
-
Save swalahamani/a1cf3cb3dfb2e1b67cdf6a59d3d4e412 to your computer and use it in GitHub Desktop.
JAVA JDBC TABLE EXAMPE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment