?
Solved

what is the error in code

Posted on 2003-04-01
20
Medium Priority
?
166 Views
Last Modified: 2010-04-01
<%@ page language="java" import="java.sql.*" %>
<%
Connection conn = null;
ResultSet rs = null;

/* Check we can find the database driver */
try {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e) {
  System.err.print("ClassNotFoundException: ");
  System.err.println(e.getMessage());
}

/* Try to open the database */
/* The ODBC name is charts, no username or password */
try {
  conn = DriverManager.getConnection("jdbc:odbc:db1", "", "");
}
catch (Exception e) {
  e.printStackTrace();
  System.err.print(e.getClass().getName());
  System.err.println(e.getMessage());
}

/* Select the chart parameters and values */
Statement stmt = conn.createStatement();

     String location = request.getParameter("select1");
     String name = request.getParameter("text1");
     String password = request.getParameter("password1");
     String topic = request.getParameter("text2");
     String msg = request.getParameter("textarea1");

String query= "insert into mail values(8,"+location+","+name+","+topic+","+msg +")";
out.println(query);

/*rs = stmt.executeQuery(query);*/
     
     out.println(location);
     out.println(name);
     out.println(password);
     out.println(topic);
     out.println(msg);

/* Try to close the database */
%>

0
Comment
Question by:gauravg
  • 6
  • 5
  • 4
  • +4
19 Comments
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8244409
What is the exception error you catched?
0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8244419
i saw this line got problem:

String query= "insert into mail values(8,"+location+","+name+","+topic+","+msg +")";

This should be like this:

String query= "insert into mail (id, location, name, topic, msg) values(8,"+location+","+name+","+topic+","+msg +")";

Where the syntax of Insert statement is like:

Insert into [table] ([field1], [field2], [...]) values ([value1], [value2], [...])
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 8244447
insert statement is correct . it can have both the formats. the field names are optional.if u r entering all the fields in the table no need to specify the field names.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:thanassis
ID: 8244470
where is the connection.open() and connection.close() statements?
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 8244500
connection.open is not necessary.
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 8244505
connection.open is not necessary.
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 8244536
connection.open is not necessary.
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 8244537
connection.open is not necessary.
0
 

Author Comment

by:gauravg
ID: 8244577
is     ResultSet rs = null;
required for
stmt.executeQuery(query);
0
 

Author Comment

by:gauravg
ID: 8244601
is     ResultSet rs = null;
required for
stmt.executeQuery(query);
0
 

Author Comment

by:gauravg
ID: 8244620
is     ResultSet rs = null;
required for
stmt.executeQuery(query);
0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8244655
>>is ResultSet rs = null; required for tmt.executeQuery(query);
Yes, we need to define the Resultset first, so we still need want to know what is the error returned.

regards
0
 

Author Comment

by:gauravg
ID: 8244688
org.apache.jasper.JasperException: No ResultSet was produced

this is the error
0
 
LVL 55

Accepted Solution

by:
Ryan Chong earned 100 total points
ID: 8244735
ok, try use the PreparedStatement instead of Statement , example:

Connection conn = null;
     ResultSet rs = null;
     PreparedStatement ps = null;    
     String sql = "";
     
     try {
         
          Crypt cc = new Crypt();
         
          bnDbUser = cc.decryptStringToString(bnDbUser).toString();
          bnDbPassword = cc.decryptStringToString(bnDbPassword).toString();
         
          //loading the driver
          Class.forName(bnDbDriver);
          //making the connection
          conn = DriverManager.getConnection(bnDbID, bnDbUser, bnDbPassword);
         
          if (conn == null) {
               response.sendRedirect(response.encodeRedirectURL("error.jsp"));
          }
     
     } catch(ClassNotFoundException e) {
          //System.out.println("Driver not found.");
          response.sendRedirect(response.encodeRedirectURL("error.jsp"));
          throw new UnavailableException(this, "Class not found.");
     }

...

then:

String query= "insert into mail values(8,?,?,?,?)";

          ps = conn.prepareStatement(query);

          ps.setString(1, location );
          ps.setString(2, name );          
          ps.setString(3, password );
          ps.setString(4, topic );
          ps.setString(5, msg );
         
          ps.executeUpdate();

          out.println("Record Added!");
         
          conn.commit();

0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8244738
and close the object when finished:

try {          
               if (rs != null)
               {
                    rs.close();
               }
          } catch (Exception e) {}
          try {          
               if (ps != null)
               {
                    ps.close();
               }
          } catch (Exception e) {}
          try {          
               if (conn != null)
               {
                    conn.close();
               }
          } catch (Exception e) {}

regards
0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8244745
Sorry ,change these line to below:

ps.setString(1, location );
         ps.setString(2, name );          
         ps.setString(3, topic );
         ps.setString(4, msg );
         
         
0
 
LVL 19

Assisted Solution

by:cheekycj
cheekycj earned 100 total points
ID: 8245888
I think ryancys is on the right track.. I see a problem with this line:
rs = stmt.executeQuery(query);

u are executing a query but it seems like the sql is doing an insert.

just try changing stmt.executeQuery(query) to stmt.executeUpdate(query);

CJ
0
 
LVL 6

Expert Comment

by:jarasa
ID: 8246805
Hi
Beside that you may put the stmt.executeQuery(query) you may notice that all your parameters are Strings

String location = request.getParameter("select1");
String name = request.getParameter("text1");
String password = request.getParameter("password1");
String topic = request.getParameter("text2");
String msg = request.getParameter("textarea1");

So you must pass them as Strings:

String query= "insert into mail values(8,'"+location+"','"+name+"','"+topic+"','"+msg +"')";


Have Fun

Javier
0
 
LVL 35

Expert Comment

by:girionis
ID: 10078566
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between ryancys and cheekycj

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Media Temple is thrilled to announce the launch of our new Partner Program, specifically designed to empower digital agencies and adtech platforms by offering white-glove support and exclusive hosting enhancements to optimize their sites and their c…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Integration Management Part 2
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question