- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java的JDBC中Statement与CallableStatement对象实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
JDBC Statement对象实例 以下是利用以下三种查询以及打开和关闭说明的例子:
boolean execute(String SQL) : 返回一个布尔值true,如果ResultSet对象可以被检索,否则返回false。使用这个方法来执行SQL DDL语句,或当需要使用真正的动态SQL.
int executeUpdate(String SQL) : 返回受影响的SQL语句执行的行数。使用此方法来执行,而希望得到一些受影响的行的SQL语句 - 例如,INSERT,UPDATE或DELETE语句.
ResultSet executeQuery(String SQL) : 返回ResultSet对象。当希望得到一个结果集使用此方法,就像使用一个SELECT语句.
基于对环境和数据库安装在前面的章节中做此示例代码已被写入.
复制下面的例子中JDBCExample.java,编译并运行,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
//STEP 1. Import required packages
import
java.sql.*;
public
class
JDBCExample {
// JDBC driver name and database URL
static
final
String JDBC_DRIVER =
"com.mysql.jdbc.Driver"
;
static
final
String DB_URL =
"jdbc:mysql://localhost/EMP"
;
// Database credentials
static
final
String USER =
"username"
;
static
final
String PASS =
"password"
;
public
static
void
main(String[] args) {
Connection conn =
null
;
Statement stmt =
null
;
try
{
//STEP 2: Register JDBC driver
Class.forName(
"com.mysql.jdbc.Driver"
);
//STEP 3: Open a connection
System.out.println(
"Connecting to database..."
);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println(
"Creating statement..."
);
stmt = conn.createStatement();
String sql =
"UPDATE Employees set age=30 WHERE id=103"
;
// Let us check if it returns a true Result Set or not.
Boolean ret = stmt.execute(sql);
System.out.println(
"Return value is : "
+ ret.toString() );
// Let us update age of the record with ID = 103;
int
rows = stmt.executeUpdate(sql);
System.out.println(
"Rows impacted : "
+ rows );
// Let us select all the records and display them.
sql =
"SELECT id, first, last, age FROM Employees"
;
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while
(rs.next()){
//Retrieve by column name
int
id = rs.getInt(
"id"
);
int
age = rs.getInt(
"age"
);
String first = rs.getString(
"first"
);
String last = rs.getString(
"last"
);
//Display values
System.out.print(
"ID: "
+ id);
System.out.print(
", Age: "
+ age);
System.out.print(
", First: "
+ first);
System.out.println(
", Last: "
+ last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}
catch
(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}
catch
(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
finally
{
//finally block used to close resources
try
{
if
(stmt!=
null
)
stmt.close();
}
catch
(SQLException se2){
}
// nothing we can do
try
{
if
(conn!=
null
)
conn.close();
}
catch
(SQLException se){
se.printStackTrace();
}
//end finally try
}
//end try
System.out.println(
"Goodbye!"
);
}
//end main
}
//end JDBCExample
|
现在编译上面的例子如下:
1
|
C:>javac JDBCExample.java
|
当运行JDBCExample,它会产生以下结果:
1
|
C:>java JDBCExample
|
1
2
3
4
5
6
7
8
9
|
Connecting to database...
Creating statement...
Return value is : false
Rows impacted : 1
ID: 100, Age: 18, First: Zara, Last: Ali
ID: 101, Age: 25, First: Mahnaz, Last: Fatma
ID: 102, Age: 30, First: Zaid, Last: Khan
ID: 103, Age: 30, First: Sumit, Last: Mittal
Goodbye!
|
JDBC CallableStatement对象实例 下面是利用CallableStatement连同下列getEmpName()的MySQL存储过程的例子:
请确定已经在EMP数据库中创建该存储过程。可以使用MySQL查询浏览器来完成它.
1
2
3
4
5
6
7
8
9
10
11
12
|
DELIMITER $$
DROP
PROCEDURE
IF EXISTS `EMP`.`getEmpName` $$
CREATE
PROCEDURE
`EMP`.`getEmpName`
(
IN
EMP_ID
INT
,
OUT
EMP_FIRST
VARCHAR
(255))
BEGIN
SELECT
first
INTO
EMP_FIRST
FROM
Employees
WHERE
ID = EMP_ID;
END
$$
DELIMITER ;
|
基于对环境和数据库安装在前面的章节中进行,这个范例程式码已被写入.
复制下面的例子中JDBCExample.java,编译并运行,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
//STEP 1. Import required packages
import
java.sql.*;
public
class
JDBCExample {
// JDBC driver name and database URL
static
final
String JDBC_DRIVER =
"com.mysql.jdbc.Driver"
;
static
final
String DB_URL =
"jdbc:mysql://localhost/EMP"
;
// Database credentials
static
final
String USER =
"username"
;
static
final
String PASS =
"password"
;
public
static
void
main(String[] args) {
Connection conn =
null
;
CallableStatement stmt =
null
;
try
{
//STEP 2: Register JDBC driver
Class.forName(
"com.mysql.jdbc.Driver"
);
//STEP 3: Open a connection
System.out.println(
"Connecting to database..."
);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println(
"Creating statement..."
);
String sql =
"{call getEmpName (?, ?)}"
;
stmt = conn.prepareCall(sql);
//Bind IN parameter first, then bind OUT parameter
int
empID =
102
;
stmt.setInt(
1
, empID);
// This would set ID as 102
// Because second parameter is OUT so register it
stmt.registerOutParameter(
2
, java.sql.Types.VARCHAR);
//Use execute method to run stored procedure.
System.out.println(
"Executing stored procedure..."
);
stmt.execute();
//Retrieve employee name with getXXX method
String empName = stmt.getString(
2
);
System.out.println(
"Emp Name with ID:"
+
empID +
" is "
+ empName);
stmt.close();
conn.close();
}
catch
(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}
catch
(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
finally
{
//finally block used to close resources
try
{
if
(stmt!=
null
)
stmt.close();
}
catch
(SQLException se2){
}
// nothing we can do
try
{
if
(conn!=
null
)
conn.close();
}
catch
(SQLException se){
se.printStackTrace();
}
//end finally try
}
//end try
System.out.println(
"Goodbye!"
);
}
//end main
}
//end JDBCExample
|
现在编译上面的例子如下:
1
|
C:>javac JDBCExample.java
|
当运行JDBCExample,它会产生以下结果:
1
|
C:>java JDBCExample
|
1
2
3
4
5
|
Connecting to database...
Creating statement...
Executing stored procedure...
Emp Name with ID:102 is Zaid
Goodbye!
|
最后此篇关于Java的JDBC中Statement与CallableStatement对象实例的文章就讲到这里了,如果你想了解更多关于Java的JDBC中Statement与CallableStatement对象实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在 go lang 中使用“If with a short statement”有什么好处。引用:go tour if v := math.Pow(x, n); v < lim { retur
完全错误: Warning: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT
完全错误: Warning: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT
我有三个存储过程 Sp1、Sp2 和 Sp3。 第一个 (Sp1) 将执行第二个 (Sp2) 并将返回的数据保存到 @tempTB1 中,第二个将执行第三个 (Sp3) 并将数据保存到 @tempTB
我已将 FLAG 设置为 1,并且正在执行 ARG 值应该仅为 DEV。但是我得到的是 ARG= DEV + CLIENTID 000023 // FLAG=1 000026 // I
我已将 FLAG 设置为 1,并且正在执行 ARG 值应该仅为 DEV。但是我得到的是 ARG= DEV + CLIENTID 000023 // FLAG=1 000026 // I
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
PMD告诉我 A switch with less than 3 branches is inefficient, use a if statement instead. 这是为什么呢?为什么是3?他
我刚开始学习 Racket,所以我仍在努力弄清楚这门语言的复杂性。我正在尝试在列表中实现我自己的搜索功能。如果函数找到它,则返回索引,否则返回 -1。 (define (find-index list
在 Kotlin 中,您可以使用类似于三元运算符的 if 语句。 我们可以选择做这样的事情: val x = if (isOdd) 1 else 2 但是如果我们有多个变量需要根据某些条件进行设置,那
在我的 Android 应用程序中,我尝试使用 XMLPullParser 使用以下代码读取 xml 文件: while (eventType != XmlPullParser.END_DOCUMEN
这个问题已经有答案了: Boolean expressions in Java (8 个回答) 已关闭 7 年前。 我遇到过一些情况,其中第一个似乎改变了 boolean 值,而第二个却没有!两者之间
我基本上想做的是: select * from request where id = 1 and created_at like (today's date); 但使用 Eloquent 。 我试过:
我不确定为什么会收到此代码。基本上我希望能够动态定位我的发射器,但是当我添加一个选项来检查位置并根据需要进行纠正时,我不断收到此错误。 添加的代码是 if (
何时使用语句而不是准备语句。我想语句用于没有参数的查询,但为什么不使用准备好的语句呢?对于没有参数的查询,哪个更快。 最佳答案 I suppose statement is used in queri
我必须创建一个表,如下所示 借款人(客户编号,贷款编号) 如果客户没有超过 3 笔贷款,则可以贷款。 我创建的表如下 create table borrower( customerno int(5),
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicates: Is "else if" faster than "switch() case"? What is the
typescript 版本 2.2.2 我在我的 UserRoutzr.ts 中写了这个要求 const users = require(path.join(process.cwd() + "/da
我有一个用 JPQL 编写的应用程序,它可以命中非常不同的查询(在不同的资源上)。 对于很多此类查询,我需要知道结果总数(计数),因为我没有应用任何 LIMIT/OFFSET 由于此查询的性质非常不同
我对以下 Java 语句感到困惑: ArtClass artClass0 = new ArtClass(); int int3 = 73; boolean boolean0 = artClass0.f
我是一名优秀的程序员,十分优秀!