当前位置:网站首页>使用 Preparedstatement 选择和显示记录的 JDBC 程序
使用 Preparedstatement 选择和显示记录的 JDBC 程序
2022-07-23 14:29:00 【allway2】
在之前的 JDBC 教程中,我们了解了PreparedStatement对象,后来我们开发了一个Java 程序来向表中插入记录,以及另一个程序来更新表的记录。现在,在这篇文章中,我们将发布一个 Java 程序来使用 PreparedStatement 对象选择和显示记录。
要获取表的记录,我们需要 ResultSet 对象并使用 ResultSet 的 getXxx(-) 方法,我们可以获取表的详细信息。
我们正在使用 Oracle 数据库,但您可以使用任何具有所需信息的数据库。在 Oracle 数据库中,我们有一个产品表,其中包含产品 ID、产品名称、产品价格和数量的详细信息。
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)我们要显示产品表的记录。因此,不需要从最终用户那里获取任何输入值,也不需要设置查询参数。
所需的 SQL 查询是,
SQL> SELECT pid, pname, price,
quantity FROM product;注意:- 在开发 JDBC 应用程序时,不建议在 SQL 查询中使用 *,因此不建议使用。SELECT * FROM product
显示表格记录的Java程序
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 class输出:-
102 Bed 2000.0 1
2051 Fan 999.0 2
111 Table 1500.0 1
Records displayed使用 PreparedStatement 根据给定值选择和显示记录
现在让我们开发另一个 JDBC 程序,它将根据给定的值选择行并仅显示那些行的详细信息。
必需的 SQL 查询,
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 class输出:-
Enter product ID: 102
Bed 2000.0 1
Records fetched & displayed
Enter product ID: 1000
Records not found
如果您喜欢这篇文章,请与您的朋友分享。您想分享有关上述主题的更多信息,还是您发现任何不正确的地方?让我们在评论中知道。谢谢!
边栏推荐
猜你喜欢

【Web漏洞探索】SQL注入漏洞

Compose canvas pie chart effect drawing

Wechat applet wx.hideloading() will close the toast prompt box

小程序商城如何精细化运营?

OpenIM重大优化-消息按需加载 一致性缓存 uniapp发布

Keil errors and solutions (1): fcarm - output name not specified, please check 'options for target - Utilities‘

Explain SQL optimization in detail

SQL bool盲注和时间盲注详解

零基础怎么自学软件测试?十年测试老鸟最强软件测试学习路线图

Pyinstaller+installforge multi file project software packaging
随机推荐
AXI interconnect IP核的说明及用法
pip报错Could not find a version that satisfies the...No matching distribution
【Flutter -- 布局】线性布局(Row 和 Column)
iphone 无法打开openv**文件的解决方案
Leetcode-67. binary sum
零基础怎么自学软件测试?十年测试老鸟最强软件测试学习路线图
[30. N-queen problem]
小程序商城如何精细化运营?
First deep search and first wide search of graph (realized by three methods)
pwn入门(3)堆
数组和特殊矩阵的压缩存储
Preliminary tutorial of Hezhou esp32c3 PIO Arduino development framework based on vscode
What about the reason why GOM and GEE set up a black screen and the fact that individual equipment maps are not displayed?
Sprintf and cv:: puttext
OpenCV求两个区域的交集
考过PMP对实际工作帮助大吗?
Dead beat recursion 1: recursive formula
Is PMP a great help for practical work?
图的先深搜索、图的先广搜索 (三种方法实现)
浏览器四大内核