I am wondering if I have the following code in a Java web app:
Statement st = null;
ResultSet rs = null;
try {
st = connection.createStatement();
rs = st.executeQuery(query);
And I need to transform this by using a PreparedStatement can I rewritte the code as following:
PreparedStatement ps = connection.prepareStatement(query);
System.out.println("Querying..."+ query);
ps.setString(1, query);
ResultSet rs = null;
try {
rs = ps.executeQuery(query);
When replacing this code my app wont give me any visible errors however when debug it wont run giving mentioning that could to generate a createstatement. What could be the error? Why it doesn't work?
Related
I am trying to move all query executions from Statement to PreparedStatement due to SQL injection. My original issue was with update statement, but I wanted to try it with select statement as well. When I execute the below line of code, the statement returns nothing.
String selectQuery = "select is_enabled, syllabus_id from ic_syllabus where syllabus_id=?";
PreparedStatement pstmt = conn.prepareStatement(selectQuery);
pstmt.setString(1, "25AC1CFB7C1A2CF07F176BD3A296F229");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String flag = rs.getString(1);
String sybsId = rs.getString(2);
}
I am using Oracle database and am not getting any exceptions either.
String get_date = check_in_date.getText();
String get_customer_no = customer_no.getText();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
try{
String driver ="com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3306/hotel";
String userid ="root";
String password ="tushar11";
Class.forName(driver);
conn = DriverManager.getConnection(url,userid,password);
pstmt = conn.prepareStatement("select occupantdetails.customer_name,
hoteldetails.service_detail, hoteldetails.cab_no from
occupantdetails JOIN hoteldetails ON
occupantdetails.customer_no=hoteldetails.customer_no" );
pstmt.setString(1, get_customer_no);
rst = pstmt.executeQuery();
while(rst.next()){
txt_customer_name.setText(rst.getString("customer_name"));
txt_room_no.setText(rst.getString("service_detail"));
txt_cab_no.setText(rst.getString("cab_no"));
}
}
I am new to this. As i am fetching the details it is showing the parameter error and i cannot solve this. I think i have written the right query and there might be mistake in java code.
I guess your statement should be something like
"SELECT *
FROM YourTable
WHERE customer_no = ?"
Check the tutorial for prepared statements
Following is the code.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:cse");
//Statement stmt;
ResultSet rset;
//stmt = conn.createStatement();
String sql = " Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(sql);
while (rset.next()) {
arr.add(rset.getInt("id"));
arr.add(rset.getString("first"));
arr.add(rset.getString("last"));
arr.add(rset.getInt("age"));
}
System.out.println(arr);
pst.close();
conn.close();
For the above am getting "Error: java.sql.SQLException: Driver does not support this function". What might be the problem?
You are misusing the PreparedStatement interface. When using PreparedStatements, you should prepare the statement with your query, bind all necessary parameters and then execute it without any SQL - this will cause the statement to execute the previously prepared SQL statement:
String sql = "Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(); // Note - No args in the executeQuery call
I was trying to call a method in jsp which returns a resultSet. Like,
In JSP:
OracleResultSet rs = null;
rs = getMyValuesHere();
out.println("rs:"+rs);
// rs is not null.Something like oracle.jdbc.driver.OracleResultSetImpl#1f1e1f
How can I retrieve my values here from the resultSet?
because not even entering into this while loop. ,
while(rs.next()) {
}
Method is:
<%!
public OracleResultSet getMyValuesHere()
{
OracleConnection connection = null;
OraclePreparedStatement ptmst = null;
OracleResultSet rs = null;
OracleCallableStatement cstmt = null;
StringBuffer sql = new StringBuffer();
//My query goes here
connection = (OracleConnection) TransactionScope.getConnection();
sql.append("SELECT DISTINCT something.... ");
rs = (OracleResultSet)ptmst.executeQuery();
return rs;
}
%>
I tried printing the data in getMyValuesHere()
rs = (OracleResultSet)ptmst.executeQuery();
while (rs.next())
{
// rs.getString(1)) -- I was able to print this.
}
You didn't specified the Database Driver in your program.. Include the driver in your program and then try it once
I want to change the position of the cursor to the first row but I don't know why my code is not working.when I add rs2.first():
and also I am getting this error :
This method should only be called on ResultSet objects that are scrollable (type TYPE_SCROLL_INSENSITIVE).
try{
String driver = "org.apache.derby.jdbc.ClientDriver";
Class.forName(driver).newInstance();
String url = "jdbc:derby://localhost:1527/test";
Connection conn = DriverManager.getConnection(url);
String query = "select * from APP.RANKING";
Statement stmt = conn.createStatement();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSet rs2 = stmt2.executeQuery(query);
while (rs.next()){
String BID = rs.getString("BALLOT_ID");
String CN = rs.getString("CANDIDATE_NAME");
String ROID = rs.getString("USER_ID");
Ro1_ID = ROID;
String RA = rs.getString("RANK");
int rowNum = rs.getRow();
int rowNum2;
boolean In_check = false;
while(rs2.next()){
In_ballot.addElement(BID);
}
rs2.First();
In_ballot.addElement(BID);
}
}
catch(Throwable e) {
System.err.println(e.getMessage());
}
this.InB_list.setModel(In_ballot);
By default, calling createStament() in a connection results in every ResultSet having type 'TYPE_FORWARD_ONLY' - This results in the exception you see using first().
Instead, use another versions of createStatement, like this one.
This sample of creating scrollable ResultSets in Derby might help.