当前位置:网站首页>Use Preparedstatement to select and display recorded JDBC programs
Use Preparedstatement to select and display recorded JDBC programs
2022-07-23 17:37:00 【allway2】
Prior to JDBC tutorial , We get it PreparedStatement object , Later, we developed a Java Program to insert records into the table , And another program to Update table records . Now? , In this article , We will release a Java Program to use PreparedStatement Object selection and display records .
To get the records of the table , We need to ResultSet Object and use ResultSet Of getXxx(-) Method , We can get the details of the table .
We are using Oracle database , But you can use any database with the required information . stay Oracle In the database , We have a product list , It includes products ID、 The product name 、 Details of product price and quantity .
SQL> SELECT * FROM product;
PID PNAME PRICE QUANTITY
----- -------- ------- ----------
102 Bed 2000 1
2051 Fan 999 2
111 Table 1500 1SQL> DESC product;
Name Null? Type
--------- -------- ----------
PID NOT NULL NUMBER(10)
PNAME VARCHAR2(15)
PRICE FLOAT(126)
QUANTITY NUMBER(10)We want to display the records of the product table . therefore , There is no need to get any input value from the end user , There is no need to set query parameters .
The required SQL Query is ,
SQL> SELECT pid, pname, price,
quantity FROM product; Be careful :- Developing JDBC Application time , Not recommended in SQL Use in query *, Therefore, it is not recommended to use .SELECT * FROM product
Show table records Java Program
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DisplayProductTable {
// SQL query
private static final String SELECT_PRODUCT_QUERY =
"SELECT PID, PNAME, PRICE, QUANTITY FROM PRODUCT";
public static void main(String[] args ) {
// declare variables
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// establish the connection
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:knowprogram",
"scott", "tiger");
// compile SQL query and
// store it in PreparedStatemet object
if(con != null)
ps = con.prepareStatement(SELECT_PRODUCT_QUERY);
// execute the query
if(ps != null) {
rs = ps.executeQuery();
}
// process the result
if(rs != null) {
while(rs.next()) {
System.out.println(""
+ rs.getInt("PID") +" "
+ rs.getString("PNAME") +" "
+ rs.getFloat("PRICE") +" "
+ rs.getInt("QUANTITY"));
}
}
System.out.println("\nRecords displayed");
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} // end of try-catch block
finally {
// close JDBC objects
try {
if(ps != null) ps.close();
} catch(SQLException se) {
se.printStackTrace();
}
try {
if(con != null) con.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
} //end of main
} //end of classOutput :-
102 Bed 2000.0 1
2051 Fan 999.0 2
111 Table 1500.0 1
Records displayedUse PreparedStatement Select and display records according to the given values
Now let's develop another JDBC Program , It will select rows based on the given value and display only the details of those rows .
Essential SQL Inquire about ,
SQL> SELECT pname, price, quantity
FROM product WHERE pid = 102;
PNAME PRICE QUANTITY
------- ---------- ----------
Bed 2000 1import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class SelectTest {
// SQL query
private static final String SELECT_PRODUCT_QUERY =
"SELECT PNAME, PRICE, QUANTITY FROM PRODUCT"+
" WHERE PID = ?";
public static void main(String[] args ) {
// declare variables
Scanner scan = null;
int pid = 0;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
boolean flag = false;
try {
// read input
scan = new Scanner(System.in);
if(scan != null) {
System.out.print("Enter product ID: ");
pid = scan.nextInt();
}
// establish the connection
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:knowprogram",
"scott", "tiger");
// compile SQL query and
// store it in PreparedStatemet object
if(con != null)
ps = con.prepareStatement(SELECT_PRODUCT_QUERY);
// set input value to query parameter
if(ps != null)
ps.setInt(1, pid);
// execute the query
if(ps != null)
rs = ps.executeQuery();
// process the result
if(rs != null) {
while(rs.next()) {
flag = true;
System.out.println(""
+ rs.getString("PNAME") +" "
+ rs.getFloat("PRICE") +" "
+ rs.getInt("QUANTITY"));
}
}
if(flag)
System.out.println("Records fetched & displayed");
else
System.out.println("Records not found");
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} // end of try-catch block
finally {
// close JDBC objects
try {
if(ps != null) ps.close();
} catch(SQLException se) {
se.printStackTrace();
}
try {
if(con != null) con.close();
} catch(SQLException se) {
se.printStackTrace();
}
try {
if(scan != null) scan.close();
} catch(Exception e) {
e.printStackTrace();
}
}
} //end of main
} //end of classOutput :-
Enter product ID: 102
Bed 2000.0 1
Records fetched & displayed
Enter product ID: 1000
Records not found
If you like this article , Please share with your friends . You would like to share more information about the above topics , Or do you find anything wrong ? Let us know in the comments . thank you !
边栏推荐
- xlinx pcie xvc
- Deeply understand the mode and vibration of mechanical system
- 工业物联网中的时序数据
- [flask advanced] deeply understand the endpoint of flask routing from the source code
- Food safety chocolate is also true or false? How much do you know about it
- Implementation of deep copy deepclone
- Kubernetes kubelet manages pod core process
- 食品安全|喝鲜奶可能感染结核病?带你了解什么是牛奶灭菌
- 乘风破浪!金融科技时代下的数字化转型之路
- js工具 cecp
猜你喜欢

职场3道坎:年薪30万、50万、100万

“如今,代码数已膨胀至天文级别”

isEmpty 和 isBlank 的用法区别,至少一半的人答不上来...

Transfer business append log (transaction propagation behavior)

Deeply understand the mode and vibration of mechanical system

为啥一问 JVM 就 懵B ???

网络基础设施可视化

Three things programmers want to do most | comics

工业物联网中的时序数据

面试官:如何用 Redis 实现分布式锁?
随机推荐
Food safety | eight things you must know when choosing probiotic products
基于OpenPGP的文件管理系统
单细胞文献学习(part6)--ForestFireClustering for sc sequencing combines iterative label propagation with ...
@Will multiple bean instances be created by multiple method calls of bean annotations
nVisual综合布线管理软件与网管软件的区别
"Now, the number of codes has expanded to astronomical level"
场景小小记
@Bean 注解的方法调用多次会创建多个bean 实例吗
使用 Preparedstatement 选择和显示记录的 JDBC 程序
Kubernetes kubelet manages pod core process
Element content must consist of character data or tags with correct format
[introduction series of redis] redis builds master-slave servers
Preliminary understanding of string
Interviewer: how to use redis to realize distributed locks?
[MySQL Cluster fault recovery]
Literature learning (part100) -- an introduction to autoencoders
leetcode刷题:动态规划05(不同路径 II)
PHP 中 try-catch 和 if-else 语句的区别
Pymoo learning (1): basic concepts
数智化时代文旅遇新机?中国移动咪咕造 “元宇宙第一岛”