Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JDBC and Relational Databases
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
JDBC and Relational Databases
throwing an Exception after first exceution ..
shiva mistry
Greenhorn
Posts: 10
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
When I try to Enter sid second time from command prompt it throws an Exception like Connection is closed but if i comment out the con.close() then it allows to perform another query ,I do not know what to do ?can any one help please?
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ContoDB { private Connection con = null; private PreparedStatement preSql = null; public ContoDB() { } public ContoDB(String url, String username, String password) { super(); try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean getStudentBysid(int sid) { Statement sql = null; ResultSet result = null; PreparedStatement preSql = null; try { preSql = con .prepareStatement("SELECT * FROM STUDENT WHERE SID = ? "); preSql.setDouble(1, sid); result = preSql.executeQuery(); // sql = con.createStatement(); // result = // sql.executeQuery("SELECT * FROM STUDENT WHERE GPA >= 3.9 "); while (result.next()) { System.out.println(" Student ID :" + result.getInt("SID")); System.out.println(" Studnet Name:" + result.getString("SNAME")); System.out.println(" Student GPA:" + result.getDouble("GPA")); System.out.println(" Student HsSize:" + result.getInt("SIZEHS")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // finally { // try { // result.close(); // preSql.close(); // sql.close(); // con.close(); // } catch (SQLException e) { // TODO Auto-generated catch block // e.printStackTrace(); // } // } return true; } public boolean getApplication(int sid) { boolean aplication = false; PreparedStatement preSql = null; ResultSet result = null; try { preSql = con .prepareStatement("SELECT * FROM APPLICATION WHERE SID = ?"); preSql.setInt(1, sid); result = preSql.executeQuery(); while (result.next()) { System.out.println(" Studnet ID: " + result.getInt("SID")); System.out.println(" College Name: " + result.getString("CNAME")); System.out.println(" Major: " + result.getString("MAJOR")); System.out.println(" Decision: " + result.getString("DECISION")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }// finally { // try { // con.close(); // result.close(); // preSql.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } return aplication = true; } }
import java.io.Console; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ConnectionTest { /** * @param args */ public static void main(String[] args) { ContoDB c = new ContoDB("jdbc:oracle:thin:@localhost:1521:xe", "hr", "orcl"); int i = 0; Console console = System.console(); System.out.println("Choose Option :"); String option = ""; do { System.out.println(" Press 1 for Student Info"); System.out.println(" Press 2 Student Application:"); System.out.println("Press 3 for Exit:"); int sel = Integer.parseInt(console .readLine("Enter your Selection\n:")); switch (sel) { case 1: int sid = Integer.parseInt(console .readLine("\nEnter Student ID:")); System.out .println("*******************************The info of Student**************************************"); c.getStudentBysid(sid); break; case 2: int sid1 = Integer.parseInt(console .readLine("\nEnter Student ID:")); System.out .println("*******************************The Application info of Student**************************************"); c.getApplication(sid1); break; case 3: System.out.println("Bye"); System.exit(0); i++; break; default: System.out.println("Please Enter Valid Selection:"); break; } option = console .readLine("\nPress Yes to continue or Any Other Key to Exit."); } while (i == 0 && option.equalsIgnoreCase("yes")); } }
Paul Clapham
Marshal
Posts: 27211
87
I like...
posted 9 years ago
2
Number of slices to send:
Optional 'thank-you' note:
Send
Don't close the connection, then.
I love a good mentalist. And so does this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Applet blinking, DB doesn't work
guestbook, looping, beans and JSTL
Getting an Exception like NoClassDefFoundError
giving ClassCastException while deploying ejb2.0 Bmp application on jboss4.5
JSTL for loop error
More...