Wednesday 15 February 2012

java - Trouble retrieving data with JDBC in Eclipse IDE -


I am quite new to Java, so I work on a project to develop my knowledge with database and Java I am here.

I have come to know how to add questions to the database, but now I am getting errors in trying to print them.

Assume that I already have everything that is scanner and SQL statement

This is my connection class which is named Main Class:

  Public Static Connection Mill Connection () throws exceptions {String driver = "com.mysql.jdbc.Driver"; String url = "jdbc: mysql: // localhost: 3306 / testTable"; String user name = "placeholder"; String password = "placeholder"; Class.forName (driver); Connection Conn = Driver Manager.getConnection (URL, Username, Password); Return canon; }  

Now in a different category if the user types! Search and a word I want to define the term that should be derived from the table with the name and column words, definition:

  string user search = user_input.next (); String [] userSearchSplit = userSearch.split ("", 3); If (userSearchSplit [0] .equals ("lookup")) {try {conn = MainClass.getConnection (); String query = "Choose the definition of the word in which the word =" + userSearchSplit [1]; Result result = pstmt.executeQuery (query); While (result.next ()) {string definition = result .getString ("definition"); Println (definition); }} Hold (exception e) {e.printStackTrace (); } Finally {try {pstmt.close (}}} Grip (SQLException e) {E.PintstacksTrace (); } Try {conn.close (); } Hold (SQLException e) {e.printstaxtrace (); }}}  

When I try to find a word, then I put it on the table:

  java Lang.NullPointerException  

Check if your user_input is empty or not?

I am assuming your code:

  result result = pstmt.executeQuery (query);  

As

  Description pstmt = conn.createStatement (); Result result = pstmt.executeQuery (query);  

Or it may be that you have not started pstmt properly


No comments:

Post a Comment