namespace JobPortal.DB;
context cdsArtifact {
/*@@layout{"layoutInfo":{"x":32,"y":121.5}}*/
entity Job {
key JOB_ID : String(10) not null;
DESCRIPTION : String(50) not null;
DEPARTMENT : String(20);
OWNER_NAME : String(50) not null;
association : association[1, 1..*] to cdsArtifact.Enrollment { JOB_ID };
};
/*@@layout{"layoutInfo":{"x":-444,"y":105.5}}*/
entity Enrollment {
key JOB_ID : String(20) not null;
key PERSON_ID : String(20) not null;
FIRST_NAME : String(20) not null;
LAST_NAME : String(20) not null;
EMAIL : String(40) not null;
LOCATION : String(20);
};
};
{
"format_version": 1,
"imports":
[ {
"target_table" : "JobPortal.DB::cdsArtifact.Job",
"source_data" : { "data_type" : "CSV", "file_name" : "JobPortal.DB::jobs.csv", "has_header" : false },
"import_settings" : { "import_columns" : ["JOB_ID","DESCRIPTION","OWNER_NAME" ] },
"column_mappings" : {"JOB_ID" : 1,"DESCRIPTION" : 2,"OWNER_NAME" : 3}
}
]
}
{
"format_version": 1,
"imports":
[ {
"target_table" : "JobPortal.DB::cdsArtifact.Enrollment",
"source_data" : { "data_type" : "CSV", "file_name" : "JobPortal.DB::enrollments.csv", "has_header" : false },
"import_settings" : { "import_columns" : ["JOB_ID", "PERSON_ID", "FIRST_NAME","LAST_NAME","EMAIL"] },
"column_mappings" : {"JOB_ID": 1, "PERSON_ID" : 2, "FIRST_NAME" : 3,"LAST_NAME" :4,"EMAIL" :5}
}
]
}
"JB101","desc1","Shobhit"
"JB102","desc2","Shobhit"
"JB103","desc3","Shobhit"
"JB104","desc4","Shobhit"
"JB101","I335136","Shobhit","Pathak","shobhit.pathak@sap.com"
"JB101","I335137","Alex","Pattrick","shobhit.pathak@sap.com"
"JB102","I335137","Shaan","Shaahid","shobhit.pathak@sap.com"
"JB104","I335136","Shobhit","Pathak","shobhit.pathak@sap.com"
service
{
"JobProtal.DB::cdsArtifact.Job" as "Jobs" navigates ("Job_Enrollments" as "HasEnrollments");
"JobProtal.DB::cdsArtifact.Enrollment" as "Enrollments" ;
association "Job_Enrollments" principal "Jobs"("JOB_ID")
multiplicity "1" dependent "Enrollments"("JOB_ID") multiplicity "*";
}
package com.sap.springLayer.javaSpringLayer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONArray;
import org.json.JSONObject;
import java.sql.DriverManager;
@Controller
@EnableAutoConfiguration
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "text/plain")
@ResponseBody
String home() {
StringBuilder builder = new StringBuilder();
builder.append("Hello World !!");
String data="";
builder.append("\n\nJDBC connection available: ");
try {
Connection conn = getConnection();
if (conn != null) {
builder.append("yes");
builder.append("\n\nCurrent Hana DB user:\n");
String userName = getCurrentUser(conn);
builder.append(userName);
builder.append("\n\nCurrent Hana schema:\n");
builder.append(getCurrentSchema(conn));
data=createJobs(conn);
} else {
builder.append("no");
}
} catch (SQLException e) {
builder.append("no");
}
return builder.append(data).toString();
}
private String getCurrentUser(Connection conn) throws SQLException {
String currentUser = "";
PreparedStatement prepareStatement = conn.prepareStatement("SELECT CURRENT_USER \"current_user\" FROM DUMMY;");
ResultSet resultSet = prepareStatement.executeQuery();
int column = resultSet.findColumn("current_user");
while (resultSet.next()) {
currentUser += resultSet.getString(column);
}
return currentUser;
}
private String getCurrentSchema(Connection conn) throws SQLException {
String currentSchema = "";
PreparedStatement prepareStatement = conn.prepareStatement("SELECT CURRENT_SCHEMA \"current_schema\" FROM DUMMY;");
ResultSet resultSet = prepareStatement.executeQuery();
int column = resultSet.findColumn("current_schema");
while (resultSet.next()) {
currentSchema += resultSet.getString(column);
}
return currentSchema;
}
private String createJobs(Connection conn) throws SQLException {
String currentSchema = "";
StringBuilder log = new StringBuilder();
String insertTableSQL1 ="INSERT INTO DATABASE_1"+"."+"\"JobProtal.DB::cdsArtifact.Job\""
+ "(JOB_ID, DESCRIPTION, DEPARTMENT, OWNER_NAME) VALUES"
+ "(?,?,?,?)";
StringBuilder builder1 = new StringBuilder();
try{
PreparedStatement preparedStatement = conn.prepareStatement(insertTableSQL1);
preparedStatement.setString(1, "JB0987");
preparedStatement.setString(2, "SAP architectRole");
preparedStatement.setString(3, "SAP JDBC HANA");
// preparedStatement.setString(4, "Tomorrow");
preparedStatement.setString(4, "ShobhitPathak");
preparedStatement.executeUpdate();
builder1.append("\n\nRecords Inserted:\n");
System.out.println("Records Inserted");
return builder1.toString();
}
catch(Exception e) {
System.out.println(e.getMessage());
builder1.append(e.getMessage());
return builder1.toString();
}
}
private Connection getConnection() {
Connection conn = null;
String DB_USERNAME = "";
String DB_PASSWORD = "";
String DB_HOST = "";
String DB_PORT = "";
try {
JSONObject obj = new JSONObject(System.getenv("VCAP_SERVICES"));
JSONArray arr = obj.getJSONArray("hanatrial");
DB_USERNAME = arr.getJSONObject(0).getJSONObject("credentials").getString("user");
DB_PASSWORD = arr.getJSONObject(0).getJSONObject("credentials").getString("password");
DB_HOST = arr.getJSONObject(0).getJSONObject("credentials").getString("host").split(",")[0];
DB_PORT = arr.getJSONObject(0).getJSONObject("credentials").getString("port");
String DB_READ_CONNECTION_URL = "jdbc:sap://" + DB_HOST + ":" + DB_PORT;
conn = (Connection) DriverManager.getConnection(DB_READ_CONNECTION_URL, DB_USERNAME, DB_PASSWORD);
} catch (Exception e) {
System.out.println("Connection Error"+e.getMessage());
System.out.println("SHOBHIT");
}
return conn;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloController.class, args);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
11 | |
10 | |
8 | |
8 | |
7 | |
6 | |
6 | |
5 | |
5 |