0

I am working with preparedStmts and resultSets for the first time on an assignment, when I run this program, it runs to completion, but it does not add anything to the sql table. We must use those two together and I have scoured the internet to try to figure out how to get this to work, but am still having problems. Any help would be great, thanks.

<%
int result = 0;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
  conn = JdbcManager.getConnection();
  String sqlcmd = "select * from APP.STOCK_TBL";
  stmt = conn.prepareStatement(sqlcmd, rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_UPDATABLE, rs.HOLD_CURSORS_OVER_COMMIT);
  String symbol = request.getParameter("SYMBOL");
  String name = request.getParameter("NAME");
  rs = stmt.executeQuery();

  while(rs.next()){

      if(rs.getString("SYMBOL").equals( rs.getString(1))){

          rs.updateString("SYMBOL", rs.getString("SYMBOL"));
          rs.updateString("NAME", rs.getString("NAME"));
          rs.updateRow();

          }
      if(!(rs.getString("SYMBOL").equals(rs.getString(1)))){

            if(rs.isAfterLast()){

                rs.moveToInsertRow();
                rs.updateString(1, rs.getString("SYMBOL"));
                rs.updateString(2, rs.getString("NAME"));
                rs.insertRow();

            }
          }
      }
%>

Edit* Sorry forgot to explain issue, when I run this program, it runs to completion, but it does not add anything to the sql table. Edit** Updated code

Beartato327
  • 97
  • 2
  • 6
  • 1
    Maybe you should explain what problems you're having. – Kayaman Apr 05 '17 at 15:37
  • Sorry, I forgot to explain the issue, I edited and posted it on the bottom. – Beartato327 Apr 05 '17 at 15:40
  • Why would a SELECT statement *add* data to the table? You sure your query is correct for what you want to accomplish? – Makoto Apr 05 '17 at 15:42
  • How about change your `if(rs.getString("SYMBOL") != rs.getString(1)){` to `else`. I guess the same column value can not read twice, maybe. – tkhm Apr 05 '17 at 15:42
  • See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Kayaman Apr 05 '17 at 15:44
  • As @Kayaman indicated, you are comparing Strings incorrectly. – GriffeyDog Apr 05 '17 at 15:45
  • I totally forgot about that and corrected it and now when I run it, it still does not add or update. Edited code above for clarity of changes. – Beartato327 Apr 05 '17 at 15:51
  • Your code makes zero sense. Why are you updating columns with the same value those columns already have? – Mark Rotteveel Apr 05 '17 at 20:06
  • @MarkRotteveel , to explain further what this web application does, the user submits a stocks ticker and the name of the stock. When they submit, it is suppose to search the SQL DB if it runs across the ticker symbol is a match it is suppose to update the name of the symbol, if it does not exist it is suppose to insert the symbol and name. That is why it seems redundant, but I feel like I am on the right track to it. – Beartato327 Apr 05 '17 at 20:46
  • Updating a column with the value it already has is useless and only has unnecessary overhead. – Mark Rotteveel Apr 06 '17 at 09:38
  • @MarkRotteveel, that is a very good point, thanks for the suggestion! – Beartato327 Apr 06 '17 at 14:18

1 Answers1

0

you could use this example in order to insert values to the sql table:

String name, last_name, phone;
PreparedStatement stmt = conn
                        .prepareStatement("INSERT INTO `admins` ( `name`, `last_name`,`phone`) VALUES (?,?,?)");

                stmt.setString(1, name); 

                stmt.setString(2, lastname);

                stmt.setString(3, phone);  stmt.executeUpdate();
Syne
  • 1
  • 3
  • yes I did that in a previous assignment, but we have to use resultSet with the preparedStatement with this one, thanks for the suggestion! – Beartato327 Apr 05 '17 at 16:11