Results 1 to 20 of 38
- 11-18-2010, 03:07 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
Jdbc Prepared Statement execute()
Hi, I have written the following code:
Java Code:package com.nitish.jdbc.preparedstatement; import java.sql.*; import java.io.*; public class Jdbc13{ public static void main(String []s)throws Exception{ System.setProperty("jdbc.drivers","oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","nitish"); String query = " "; PreparedStatement ps = con.prepareStatement(query); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(!query.equals("exit")){ try{ query = br.readLine(); Boolean b = ps.execute(); if(b){ ResultSet rs = ps.getResultSet(); while(rs.next()){ System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getDouble(3)); }//while }//if else{ System.out.println("Enter Student ID :: "); int stid=Integer.parseInt(br.readLine()); System.out.println("Enter Student Name :: "); String name=br.readLine(); System.out.println("Enter Student Fee :: "); double stfee = Double.parseDouble(br.readLine()); ps.setInt(1,stid); ps.setString(2,name); ps.setDouble(3,fee); int count = ps.getUpdateCount(); System.out.println("Rows Affected::"+count); }//else }//try catch(Exception e){} }//while }//main }//class
Moderator Edit: Code tags addedLast edited by Fubarable; 11-18-2010 at 04:03 AM. Reason: Moderator Edit: Code tags added
-
- 11-18-2010, 03:28 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
- 11-18-2010, 03:33 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
And if I don't encapsulate those codes into try block, then when enter exit on command line it will raise an SQL exception saying invalid SQL type.
-
-
Shoot man, you were instructed not to ignore exceptions in your previous thread from 10/28. This is advice you need to listen to!
- 11-18-2010, 03:49 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
- 11-18-2010, 03:56 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
And everytime I have to press ctrl+break to come out of it.
- 11-18-2010, 07:42 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,571
- Rep Power
- 12
- 11-18-2010, 08:46 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
That code is completely messed up.
You are not (as masijade shows) creating your PreparedStatement with any SQL at all.
You do read something that you then assign to query, but that is after you have created the PreparedStatement. And then there are no parameters assigned to it, so there really is no need for it to be a PreparedStatement in the first place.
Then (in the else) you assign some data to the PreparedStatement (which at this point still has no query associated with it) and executeUpdate.
None of that will work.
- 11-18-2010, 09:09 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
- 11-18-2010, 09:17 AM #12
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,571
- Rep Power
- 12
Of course, why don't you show your attempt at it and post the entire error/compiler message(s) you are getting from it. (along with examples of your "input" if necessary)
-
I'm suspecting that we're dealing with a troll here.
- 11-18-2010, 02:43 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
I don't know.
3 threads in 2 or 3 months isn't really troll-like.
- 11-18-2010, 05:29 PM #15
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
- 12-09-2010, 10:30 AM #16
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
I tried but couldn't succeed. For a parameterized SQL statement it should prompt for entering details but it is not doing so. Here's the code:
Java Code:package com.stech.jdbc.preparedstatement; import java.sql.*; import java.io.*; public class Jdbc13{ public static void main(String []s)throws Exception{ System.setProperty("jdbc.drivers","oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","nitish"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String query=" "; while(!query.equals("exit")){ query = br.readLine(); PreparedStatement ps = con.prepareStatement(query); Boolean b = ps.execute(); if(b){ ResultSet rs = ps.getResultSet(); while(rs.next()){ System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getDouble(3)); }//while }//if else{ System.out.println("Enter Student ID :: "); int stid=Integer.parseInt(br.readLine()); System.out.println("Enter Student Name :: "); String name=br.readLine(); System.out.println("Enter Student Fee :: "); double stfee = Double.parseDouble(br.readLine()); ps.setInt(1,stid); ps.setString(2,name); ps.setDouble(3,stfee); int count = ps.getUpdateCount(); System.out.println("Rows Affected::"+count); }//else }//while }//main }//class
Last edited by nitishjtm; 12-09-2010 at 06:26 PM.
- 12-09-2010, 11:11 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
How are you determining whether a query requires parameters?
You're reading the query in from the command line presumably, so what is supposed to happen?
Is the user supposed to type in the SQL?
- 12-09-2010, 06:21 PM #18
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
I have used execute() of prepared statement. From command line I am passing query. It works fine for a select statement.
When I am giving a query as:
insert into student_details values(1,'Nitish',5000)
then also it works.
But I want it to work when query given should be as:
insert into student_details values(?,?,?)
On giving this query it should prompt for entering the respective details, and that its not doing.
Did I did any mistake there then please help me to find it out.Last edited by nitishjtm; 12-09-2010 at 06:25 PM.
- 12-10-2010, 08:31 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
Then do a indexOf() on the sql string to see if there are any '?'. If there are then do your else stuff. Don't execute the prepared statement until after the if/else.
Of course this is not exactly going to be robust, but I'm guessing this is not a business app.
- 12-10-2010, 08:41 AM #20
Similar Threads
-
JDBC Prepared Statement
By Floetic in forum JDBCReplies: 4Last Post: 05-20-2009, 11:53 PM -
MySQL/JDBC Prepared Statement Select query
By thelinuxguy in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 05:29 PM -
MySQL/JDBC prepared statement problem
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 02-11-2009, 11:21 PM -
Using Prepared Statement
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:22 AM
Bookmarks