Passing variable value to cassandra cql statement [duplicate] - java

This question already has answers here:
Passing parameter to Cassandra CQL query using DataStax client
(2 answers)
Closed 9 years ago.
I have Java code to access and process data in Cassandra. How do I pass a Java variable to Cassandra CQL query written in Java. Here is the code:
My code goes like this:
itemname="Item01";
com.datastax.driver.core.PreparedStatement result =
session.execute("select itemname from demodb.retail_transaction where itemnamw = itemname;");
But it gives the following error:
no viable alternative at input ';'

i think you should bind your variables to the prepared statement like this:
itemname="Item01";
itemtype="type01";
com.datastax.driver.core.PreparedStatement result =
session.execute("select itemname from demodb.retail_transaction where itemnamw = ? and
itemtype =?;");
BoundStatement boundStatement = new BoundStatement(statement);
ResultSet results = session.execute(boundStatement.bind(
itemname,itemtype));

Related

why do I get a syntax error for prepared statement? [duplicate]

This question already has answers here:
preparedStatement syntax error
(2 answers)
Closed 4 years ago.
I have written a prepared statement but its giving a syntax error at ?. I am not able to understand whats wrong.It should pass a movie name and get the result as directors of that movie
stmt=getConnection().createStatement();
String sql="SELECT directors FROM moviedata WHERE moviedata.title = ?";
PreparedStatement preparedStatement=conn.prepareStatement(sql);
preparedStatement.setString(1,movieName);
rs=preparedStatement.executeQuery(sql);
The problem is here:
rs=preparedStatement.executeQuery(sql);
You shouldn't pass the SQL String to executeQuery(), since the prepared statement already contains the SQL String with the ? placeholder replaced by the value of movieName.
Use:
rs=preparedStatement.executeQuery();
The problem is not in using Preparedstatement >Because the prepared statement object holds the sql query. The why again unnecessarily passing Sql string to executequery() method.PreparedStatement preparedstatement=conn.PrepareStatement(sql). This holds the sql query and when executequery() method is called The query is evaluated.

How to read Stored Procedure source code from a java program? [duplicate]

This question already has answers here:
How to see PL/SQL Stored Function body in Oracle
(3 answers)
Closed 5 years ago.
I need to read the contents(text inside a stored procedure) of a stored procedure of a particular schema from a Java program.
Example. suppose I have a sp in oracle named mySp.
I need to read the text inside the sp.
How i can achieve this from a java program.
I think you can use something like this..Better refer JDBC MetaData API..Hope this helps
http://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html
DatabaseMetaData dbMetaData = conn.getMetaData();
ResultSet rs = dbMetaData.getProcedureColumns(conn.getCatalog(),
null,
"mySp",
"");
while(rs.next()) {
/
}

How to get all type names from a database using Java Swing? [duplicate]

This question already has answers here:
How to get all trigger names from a database using Java JDBC?
(4 answers)
Closed 7 years ago.
I'd like to retrieve all trigger names from an oracle database schema.
I use getFunctions to retrieve all functions but i can't find another one for the types.
DatabaseMetaData dbmd;
ResultSet result = dbmd.getFunctions(null, Ousername, null);
Here is some created types :
create type FINAL_obj is object (acode integer,performance Float);
create type FINAL_tab is table of FINAL_obj;
You can do it using metadata.
DatabaseMetaData dbmd = dbConnection.getMetaData();
ResultSet result = dbmd.getTables("%", Ousername, "%", new String[]{ "TYPE" });
while (result.next()) {
result.getString("TABLE_NAME")
}

How to see PreparedStatement's SQL string? [duplicate]

This question already has answers here:
Get query from java.sql.PreparedStatement [duplicate]
(8 answers)
Closed 9 years ago.
When we create a PreparedStatement we use '?' chars to then be replaced by setted parameters.
How can we see the final SQL string after these parameters are set?
There is no final SQL string, the version with placeholders is what is actually sent to server. The data is sent completely separately from it as you execute queries on the prepared statement.
You can log the string with placeholders, and then each dataset individually.
Your code could combine them in the log to an actual SQL string if that's what you want:
String query = "SELECT a FROM b WHERE c = ?";
...
pstmt.setString(1, "asd");
logSQL( query, "asd");
logSQL would then actually log "SELECT a FROM b WHERE c = 'asd'". Could be that someone has actually implemented this before...
The easiest way (that works with any JDBC driver) is to use log4jdbc. It's a proxy that wraps the driver, creates a readable SQL string by combining the SQL and its parameters and logs it, then passes the SQL and parameters on to the underlying driver.

Get the Query Fired using PreparedStatement with the bind variables on Oracle Database [duplicate]

This question already has answers here:
Get query from java.sql.PreparedStatement [duplicate]
(8 answers)
Closed 10 years ago.
EDIT: I am using Oracle
I am Writing a dynamic query using PreparedStatement which goes something like this
String query="UPDATE <tablename> SET
column1=?,column2=?,.....,coulmn7=? WHERE "
+ "column5=TO_DATE(?,'DD/MM/YYYY') AND "
+ "column6=? AND "
....
+ "column7=?";
PreparedStatement prest2=con2.PrepareThisStatement(query);
while(true){
//loop through and bind values
if(end of list) break;
}
prest2.executeUpdate(query);
I need to keep the track of the queries that get fired at the database and log them in a table.
But I cannot get the entire query when using the PreparedStatement, nor can I store the values in a separate field. Is there a way to get the final query that was fired at the database.
You Can, using 1prest2.toString();

Categories