Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c39763

Browse files
Merge pull request #9 from oracle/2DaysJavaGuide-Workspace
2 days java guide workspace
2 parents e146cac + 0bb801f commit 1c39763

File tree

18 files changed

+1581
-1
lines changed

18 files changed

+1581
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.oracle.jdbc.samples.bean;
7+
8+
import java.util.List;
9+
import com.oracle.jdbc.samples.entity.Employee;
10+
11+
/**
12+
*
13+
* @author nirmala.sundarappa@oracle.com
14+
*/
15+
public interface JdbcBean {
16+
/**
17+
* Get a list of Employees
18+
* @return List of employees
19+
*/
20+
public List<Employee> getEmployees();
21+
22+
/**
23+
* Get List of employee based on empId. This will always return one row
24+
* but returning a List to be make signatures consistent.
25+
* @param empId
26+
* @return
27+
*/
28+
public List<Employee> getEmployee(int empId);
29+
30+
/**
31+
* Update employee based on employee-id. Returns the updated record.
32+
* @param empId
33+
* @return updated record.
34+
*/
35+
public Employee updateEmployee(int empId);
36+
37+
/**
38+
* Get List of employees by First Name pattern
39+
* @param fn
40+
* @return List of employees with given beginning pattern
41+
*/
42+
public List<Employee> getEmployeeByFn(String fn);
43+
44+
/**
45+
* Increment salary by a percentage
46+
* @param incrementPct percent increase
47+
* @return List of employees with incremented salary
48+
*/
49+
public List<Employee> incrementSalary(int incrementPct);
50+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.oracle.jdbc.samples.bean;
2+
3+
import java.sql.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
import com.oracle.jdbc.samples.entity.Employee;
9+
import oracle.jdbc.OracleTypes;
10+
11+
import java.sql.PreparedStatement;
12+
import oracle.jdbc.OracleStatement;
13+
import oracle.jdbc.OracleConnection;
14+
15+
import oracle.jdbc.driver.OracleDriver;
16+
17+
/**
18+
*
19+
* @author nirmala.sundarappa@oracle.com
20+
*/
21+
public class JdbcBeanImpl implements JdbcBean {
22+
23+
public static Connection getConnection() throws SQLException {
24+
//
25+
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
26+
//
27+
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@//myorclhost:1521/myorcldbservice", "hr", "hr");
28+
29+
return connection;
30+
}
31+
32+
@Override
33+
public List<Employee> getEmployees() {
34+
List<Employee> returnValue = new ArrayList<>();
35+
try (Connection connection = getConnection()) {
36+
try (Statement statement = connection.createStatement()) {
37+
try (ResultSet resultSet = statement.executeQuery("SELECT Employee_Id, First_Name, Last_Name, Email, Phone_Number, Job_Id, Salary FROM EMPLOYEES")) {
38+
while(resultSet.next()) {
39+
returnValue.add(new Employee(resultSet));
40+
}
41+
}
42+
}
43+
} catch (SQLException ex) {
44+
logger.log(Level.SEVERE, null, ex);
45+
ex.printStackTrace();
46+
}
47+
48+
return returnValue;
49+
}
50+
51+
/**
52+
* Returns the employee object for the given empId. Returns
53+
* @param empId
54+
* @return
55+
*/
56+
@Override
57+
public List<Employee> getEmployee(int empId) {
58+
List<Employee> returnValue = new ArrayList<>();
59+
60+
try (Connection connection = getConnection()) {
61+
try (PreparedStatement preparedStatement = connection.prepareStatement(
62+
"SELECT Employee_Id, First_Name, Last_Name, Email, Phone_Number, Job_Id, Salary FROM EMPLOYEES WHERE Employee_Id = ?")) {
63+
preparedStatement.setInt(1, empId);
64+
try (ResultSet resultSet = preparedStatement.executeQuery()) {
65+
if(resultSet.next()) {
66+
returnValue.add(new Employee(resultSet));
67+
}
68+
}
69+
}
70+
} catch (SQLException ex) {
71+
logger.log(Level.SEVERE, null, ex);
72+
ex.printStackTrace();
73+
}
74+
75+
return returnValue;
76+
}
77+
78+
@Override
79+
public Employee updateEmployee(int empId) {
80+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
81+
}
82+
83+
@Override
84+
public List<Employee> getEmployeeByFn(String fn) {
85+
List<Employee> returnValue = new ArrayList<>();
86+
87+
try (Connection connection = getConnection()) {
88+
try (PreparedStatement preparedStatement = connection.prepareStatement(
89+
"SELECT Employee_Id, First_Name, Last_Name, Email, Phone_Number, Job_Id, Salary FROM EMPLOYEES WHERE First_Name LIKE ?")) {
90+
preparedStatement.setString(1, fn + '%');
91+
try (ResultSet resultSet = preparedStatement.executeQuery()) {
92+
while(resultSet.next()) {
93+
returnValue.add(new Employee(resultSet));
94+
}
95+
}
96+
}
97+
} catch (SQLException ex) {
98+
logger.log(Level.SEVERE, null, ex);
99+
ex.printStackTrace();
100+
}
101+
102+
return returnValue;
103+
}
104+
105+
@Override
106+
public List<Employee> incrementSalary (int incrementPct) {
107+
List<Employee> returnValue = new ArrayList<>();
108+
109+
try (Connection connection = getConnection()) {
110+
try (CallableStatement callableStatement = connection.prepareCall("begin ? := refcur_pkg.incrementsalary(?); end;")) {
111+
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
112+
callableStatement.setInt(2, incrementPct);
113+
callableStatement.execute();
114+
try (ResultSet resultSet = (ResultSet) callableStatement.getObject(1)) {
115+
while (resultSet.next()) {
116+
returnValue.add(new Employee(resultSet));
117+
}
118+
}
119+
}
120+
} catch (SQLException ex) {
121+
logger.log(Level.SEVERE, null, ex);
122+
ex.printStackTrace();
123+
}
124+
125+
return returnValue;
126+
}
127+
128+
static final Logger logger = Logger.getLogger("com.oracle.jdbc.samples.bean.JdbcBeanImpl");
129+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.oracle.jdbc.samples.entity;
7+
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
import java.sql.Timestamp;
11+
12+
/**
13+
*
14+
* @author nirmala.sundarappa@oracle.com
15+
*/
16+
public class Employee {
17+
18+
private int Employee_Id;
19+
private String First_Name;
20+
private String Last_Name;
21+
private String Email;
22+
private String Phone_Number;
23+
private String Job_Id;
24+
private int Salary;
25+
26+
public Employee(ResultSet resultSet) throws SQLException {
27+
this.Employee_Id = resultSet.getInt(1);
28+
this.First_Name = resultSet.getString(2);
29+
this.Last_Name = resultSet.getString(3);
30+
this.Email = resultSet.getString(4);
31+
this.Phone_Number = resultSet.getString(5);
32+
this.Job_Id = resultSet.getString(6);
33+
this.Salary = resultSet.getInt(7);
34+
}
35+
36+
public int getEmployee_Id() {
37+
return Employee_Id;
38+
}
39+
40+
public void setEmployee_Id(int Employee_Id) {
41+
this.Employee_Id = Employee_Id;
42+
}
43+
44+
public String getFirst_Name() {
45+
return First_Name;
46+
}
47+
48+
public void setFirst_Name(String First_Name) {
49+
this.First_Name = First_Name;
50+
}
51+
52+
public String getLast_Name() {
53+
return Last_Name;
54+
}
55+
56+
public void setLast_Name(String Last_Name) {
57+
this.Last_Name = Last_Name;
58+
}
59+
60+
public String getEmail() {
61+
return Email;
62+
}
63+
64+
public void setEmail(String Email) {
65+
this.Email = Email;
66+
}
67+
68+
public String getPhone_Number() {
69+
return Phone_Number;
70+
}
71+
72+
public void setPhone_Number(String Phone_Number) {
73+
this.Phone_Number = Phone_Number;
74+
}
75+
76+
public String getJob_Id() {
77+
return Job_Id;
78+
}
79+
80+
public void setJob_Id(String Job_Id) {
81+
this.Job_Id = Job_Id;
82+
}
83+
84+
public int getSalary() {
85+
return Salary;
86+
}
87+
88+
public void setSalary(int Salary) {
89+
this.Salary = Salary;
90+
}
91+
92+
93+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.oracle.jdbc.samples.web;
7+
8+
import javax.servlet.ServletException;
9+
import javax.servlet.annotation.WebServlet;
10+
import javax.servlet.http.HttpServlet;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
import java.io.IOException;
14+
15+
/**
16+
*
17+
* @author nirmala.sundarappa@oracle.com
18+
*/
19+
@WebServlet(name = "GetRole", urlPatterns = {"/getrole"})
20+
/*
21+
@ServletSecurity(
22+
httpMethodConstraints = {
23+
@HttpMethodConstraint(value = "GET", rolesAllowed = "staff"),
24+
@HttpMethodConstraint(value = "GET", rolesAllowed = "manager"),
25+
@HttpMethodConstraint(value = "POST", rolesAllowed = "manager",
26+
transportGuarantee = NONE),
27+
}
28+
)
29+
*/
30+
public class GetRole extends HttpServlet {
31+
32+
private static final String[] ROLES = {"manager", "staff" };
33+
34+
/**
35+
* Handles the HTTP <code>GET</code> method.
36+
*
37+
* @param request servlet request
38+
* @param response servlet response
39+
* @throws ServletException if a servlet-specific error occurs
40+
* @throws IOException if an I/O error occurs
41+
*/
42+
@Override
43+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
44+
throws ServletException, IOException {
45+
46+
response.setContentType("text/css");
47+
String returnValue = "anonymous";
48+
for (String role : ROLES) {
49+
if(request.isUserInRole(role)) {
50+
returnValue = role;
51+
break;
52+
}
53+
}
54+
55+
response.getWriter().print(returnValue);
56+
}
57+
58+
59+
/**
60+
* Returns a short description of the servlet.
61+
*
62+
* @return a String containing servlet description
63+
*/
64+
@Override
65+
public String getServletInfo() {
66+
return "JdbcWebServlet: Reading Employees table using JDBC and transforming it as a JSON.\n Author: nirmala.sundarapp@oracle.com";
67+
}// </editor-fold>
68+
69+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /