Skip to content

Commit ca96ee8

Browse files
Added sample code and accompanying script for PreparedStatements and JDBC (#199)
1 parent a12b3c9 commit ca96ee8

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.*/
2+
3+
/**
4+
* DESCRIPTION
5+
*
6+
* A simplified sample of Create, Read, Update and Delete (CRUD) operations using
7+
* PreparedStatements to demonstrate connection to Oracle Database XE and get
8+
* users running queries against their database as quickly as possible. The following
9+
* code was written with brevity, simplicity and minimalism in mind and is not meant
10+
* for production purposes.
11+
*
12+
* DEPENDENCIES/REQUIREMENTS
13+
*
14+
* A. This sample expects the simplified table, ISSUES, to exist. The setup script
15+
* is available in the following file:
16+
*
17+
* PreparedStatementSimplifiedSample.sql
18+
*
19+
* COMMENTS
20+
*
21+
* 1 - This is a basic CommandLineRunner Spring-boot application
22+
* 2 - jdbc:oracle:thin@//[hostname]:[port]/[DB service name] For XE local installations, the value below is the default
23+
* 3 - Inserts a new record in the ISSUES table (Can be commented out for testing)
24+
* 4 - Updates an existing record's STATUS to an arbitrary number with a given ID (Can be commented out for testing)
25+
* 5 - Delete an existing record with a given ID (Can be commented out for testing)
26+
* 6 - Prints out every record and its columns in the ISSUES table
27+
*
28+
*/
29+
30+
import oracle.jdbc.pool.OracleDataSource;
31+
import org.springframework.boot.CommandLineRunner;
32+
import org.springframework.boot.SpringApplication;
33+
import org.springframework.boot.autoconfigure.SpringBootApplication;
34+
35+
import java.sql.Connection;
36+
import java.sql.PreparedStatement;
37+
import java.sql.ResultSet;
38+
39+
40+
@SpringBootApplication // 1
41+
public class PreparedStatementSimplifiedSample implements CommandLineRunner {
42+
43+
public static void main(String[] args) {
44+
SpringApplication.run(XeApplication.class, args);
45+
}
46+
47+
@Override
48+
public void run(String... args) throws Exception {
49+
50+
// 1
51+
OracleDataSource ods = new OracleDataSource();
52+
ods.setURL("jdbc:oracle:thin:@//localhost:1521/XEPDB1"); // 2
53+
ods.setUser("[username]");
54+
ods.setPassword("[password]]");
55+
Connection conn = ods.getConnection();
56+
57+
// 3
58+
PreparedStatement insert = conn.prepareStatement("insert into ISSUES(ISSUE_TITLE, ISSUE_BY) values (?, ?)");
59+
insert.setString(1, "Requesting project access for New Hire");
60+
insert.setString(2, "Mark");
61+
insert.execute();
62+
63+
// 4
64+
PreparedStatement update = conn.prepareStatement("update ISSUES set ISSUE_STATUS=? where ISSUE_ID=?");
65+
update.setInt(1, 7);
66+
update.setInt(2, 1);
67+
update.execute();
68+
69+
// 5
70+
PreparedStatement delete = conn.prepareStatement("delete ISSUES where ISSUE_ID=?");
71+
delete.setInt(1, 1);
72+
delete.execute();
73+
74+
// 6
75+
PreparedStatement select = conn.prepareStatement("select * from ISSUES");
76+
ResultSet rslt = select.executeQuery();
77+
while (rslt.next()) {
78+
System.out.println("-----");
79+
System.out.println(" ID: " + rslt.getInt("ISSUE_ID"));
80+
System.out.println(" BY: " + rslt.getString("ISSUE_BY"));
81+
System.out.println(" ON: " + rslt.getDate("ISSUE_ON").toString());
82+
System.out.println(" ABT: " + rslt.getString("ISSUE_TITLE"));
83+
System.out.println(" STA: " + rslt.getInt("ISSUE_STATUS"));
84+
}
85+
86+
}
87+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE ISSUES (
2+
ISSUE_ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY
3+
PRIMARY KEY,
4+
ISSUE_TITLE VARCHAR2(255) NOT NULL,
5+
ISSUE_BY VARCHAR2(250) NOT NULL,
6+
ISSUE_ON DATE DEFAULT ON NULL SYSDATE NOT NULL,
7+
ISSUE_STATUS NUMBER(1) DEFAULT ON NULL 1 NOT NULL
8+
);

0 commit comments

Comments
 (0)