How to create a SDO_Geometry over Oracle-JDBC - oracle

I want to create an Oracle Spatial Geometry over JDBC with the following Statement:
//insert vorbereiten
try {
preStatement = conn.prepareStatement("insert into way(id, shape) "
+ "values(? ,SDO_GEOMETRY(2002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1), SDO_ORDINATE_ARRAY(?)))");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println("setString:");
preStatement.setString(1, "1");
Array a=conn.createArrayOf("double", new Object[]{9.23, 52.45, 9.67, 52.54});
preStatement.setArray(2, a);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But this does not work. Can someone please tell me how I can set the SDO_ORDINATE_ARRAY(?)-Values ?

Related

Unable to handle Exception properly in spring boot pubsub app

This is my code where i have problem:
try {
publisher.publish(payload).get();
} catch (com.google.api.gax.rpc.DeadlineExceededException e) {
LOGGER.error("com.google.api.gax.rpc.DeadlineExceededException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.NotFoundException e) {
LOGGER.error("com.google.api.gax.rpc.NotFoundException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.ApiException e) {
LOGGER.error("com.google.api.gax.rpc.ApiException occured: " + e.getCause());
} catch (io.grpc.StatusRuntimeException e) {
LOGGER.error("io.grpc.StatusRuntimeException occured: " + e.getCause());
} catch (java.lang.RuntimeException e) {
LOGGER.error("RuntimeException occured: " + e.getCause());
} catch (java.util.concurrent.ExecutionException e) {
LOGGER.error("java.util.concurrent.ExecutionException occured: " + e.getCause()); // my program cursor always come to this point
} catch (Exception e) {
LOGGER.error("Exception occured: " + e.getCause());
}
And the getCause of ExecutionException catch is:
java.util.concurrent.ExecutionException occured:
com.google.api.gax.rpc.NotFoundException:
io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found
If you see I already put the catch for "com.google.api.gax.rpc.NotFoundException" in second catch, so why it went into the ExcutionException catch.
Due to this nature I am not able to write proper message for the client to address.
Thanks in advance if anyone can help.
When publisher.publish(payload).get() would have been executed it would have thrown java.util.concurrent.ExecutionException ex= new com.google.api.gax.rpc.NotFoundException() i.e reference of java.util.concurrent.ExecutionException but object of com.google.api.gax.rpc.NotFoundException. So in try catch , when reference was checking which catch block to go, it could not get through api.gax.rpc.NotFoundException() as cactch only catches same or super class so it got catch later but since object was madeup of com.google.api.gax.rpc.NotFoundException so getCause got overridden and when you executed it object method was called.
Resolution: eg
try{
}
catch(Exception e){
throw (e.getClass()).cast(e);
}
where you can put this catch block in java.util.concurrent.ExecutionException.

java 8 Stream to map

I want to convert the following into functional program. Please help to stream line the below code.
Map <String, TreeSet<Double>> cusipMap = new HashMap<>();
String[] key = new String[1];
try {
Files.lines(Paths.get("C:\\CUSIP.txt")).
forEach(l -> {
if (isCUSIP(l)) {
if (cusipMap.get(l) == null )
cusipMap.put(l, new TreeSet<Double>());
key[0] = l;
} else {
cusipMap.get(key[0]).add(Double.valueOf(l));
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this one
try {
Map<String, TreeSet<Double>> result = Files.lines(Paths.get("C:\\CUSIP.txt"))
.collect(Collectors.groupingBy(Function.identity(), Collector.of(
TreeSet::new,
(TreeSet<Double> tree, String s) -> {tree.add(Double.valueOf(s));},
(TreeSet<Double> tree, TreeSet<Double> s) -> {tree.addAll(s); return tree;}
)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

How to get all the info from ClusterSearchShardsRequest

I have devised the following code to get the info similar to _search_shards rest API in ES:
ClusterSearchShardsRequest clusterSearchShardsRequest
= new ClusterSearchShardsRequest();
clusterSearchShardsRequest.routing("route2");
try {
DiscoveryNode[] discoveryNodes = client().admin().cluster()
.searchShards(clusterSearchShardsRequest)
.get()
.getNodes();
for (int i=0; i<=discoveryNodes.length; i++){
System.out.print("\n\n\n"+discoveryNodes[i].toString()+"\n\n\n");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
However this tends not to initialize the actual clusterSearchShardsRequest.
How to initialize the clusterSearchShardsRequest for the given client and index?
Simply create the new ClusterSearchShardsRequest(BOOK_INDEX_NAME) with the index name aprameter.

jTable that will listen to jDateChooser's 'textfield'?

Hello everyone I'm trying to make a scheduling system for my System and Analysis Design thesis and I am having trouble trying to connect/bind/make the jTable listen to the jDateChooser's input. Elaborately, I want my scheduling to be like this:
I choose a date in the jDateChooser
jTable will 'sort out' itself via the date inputted on the jDatechooser
is there anyway to do this?
For now all I have is a table propertyChangelistener:
private void sched_tablePropertyChangeListener(java.beans.PropertyChangeEvent evt) {
try{
String calendar = ((JTextField)jdc.getDateEditor().getUiComponent()).getText();
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/accountsDB?zeroDateTimeBehavior=convertToNull","root","");
String query = "select * from accountsdb.schedules where Date= ?";
ps= conn.prepareStatement(query);
ps.setString(1, calendar);
ResultSet rs = ps.executeQuery();
sched_table.setModel(DbUtils.resultSetToTableModel(rs));
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
} finally {
if (conn != null)
try { conn.close();
} catch (SQLException ignore) {}
if (ps != null){
try {
ps.close();
} catch (SQLException ignore){}
}
}
}
Somehow when I run my application it doesn't seem to open if that block of code is on it which means I really did do something wrong. Can anyone change or tell me what I should do or where should I start with the jTable listening to the jDatechooser thing?
~Thanks in advance for those who will answer!~
Nevermind, turns out all I had to do was change the jDateChooser's Date Format since it wasn't exactly the same formatting hence I couldn't call anything from the database. If anyone's interested on what I did I'll just leave this here
private void jdcPropertyChange(java.beans.PropertyChangeEvent evt) {
try{
String d1 = ((JTextField)jdc.getDateEditor().getUiComponent()).getText();
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/accountsDB?zeroDateTimeBehavior=convertToNull","root","");
String query = "select * from accountsdb.schedules where Date= ? order by time,timezone";
ps = conn.prepareStatement(query);
ps.setString(1, d1);
ResultSet rs = ps.executeQuery();
sched_table.setModel(DbUtils.resultSetToTableModel(rs));
} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
} finally {
if (conn != null) {
try { conn.close();
} catch (SQLException ignore) {}
}
if (ps != null){
try {
ps.close();
} catch (SQLException ignore){}
}
}
}
What this does is that everytime I pick on a date from the jDateChooser(named it jdc) the table/database calls that date and sorts it out. Which is what I wanted.

How to fix error thrown during update of scroll sensitive resultset?

I'm trying to update a row in ResultSet, but it is throwing an error. I passed the constant value ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE to createStatement.
this is my code :
public void modifyPrice(float percentage) throws SQLException {
try {
con = util.connectdb();
con.setAutoCommit(false);
st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery("select * from " + util.dbName
+ ".COFFEES");
while (rs.next()) {
float f = rs.getFloat("PRICE");
rs.updateFloat("PRICE", f * percentage);
rs.updateRow();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (st != null) {
st.close();
con.close();
}
}
}
When I executed this code block, the stack below got printed on console.
java.sql.SQLException: Invalid operation for read only resultset: updateFloat
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:131)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:197)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:261)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:269)
at oracle.jdbc.driver.BaseResultSet.updateFloat(BaseResultSet.java:236)
at oracle.jdbc.driver.OracleResultSet.updateFloat(OracleResultSet.java:677)
at tutorial.ModifyResultSet.modifyPrice(ModifyResultSet.java:29)
at tutorial.ModifyResultSet.main(ModifyResultSet.java:15)
Can anyone help me fix this error?
According to these links
http://www.coderanch.com/t/301466/JDBC/databases/Invalid-operation-read-only-resultset
http://www.coderanch.com/t/295932/JDBC/databases/updateXXX-function-ResultSet
instead of "select * from" you should use "select my_column_name from" statement.
See if it makes any difference.

Resources