[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1044
  • Last Modified:

Servlet + MySql -- UPDATE query will not work

WHY WON"T THIS WORK...
I need  a fresh view, i'm not getting anywhere anymore

public class DBupdate extends HttpServlet {
        
        public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
                       throws ServletException, IOException {
               response.setContentType("text/html");
               PrintWriter out = response.getWriter();
               String driver = "com.mysql.jdbc.Driver";
               String url = "jdbc:mysql://localhost:3306/some-db";
               String username = "some-user";
               String password = "some-password";
               String query = "UPDATE users SET users.changed_pass = \"1\" WHERE users.user_name = \"another-user\";";                       showTable(driver, url, username, password, query, out);
              response.sendRedirect("/core/index2.jsp");
              }

        public void showTable(String driver, String url,
                          String username, String password,
                          String query, PrintWriter out) {
    try {
            // Load database driver if it's not already loaded.
            Class.forName(driver);
            // Establish network connection to database.
            Connection connection = DriverManager.getConnection(url, username, password);
            Statement statement = connection.createStatement();
                statement.executeQuery(query);
                query = "SELECT * FROM users;";
            ResultSet resultSet = statement.executeQuery(query);
            // Print results by steping through each row in the result set.
          while(resultSet.next()) {
                if (resultSet.getString(1).equals("0")) {
                      firstVisit = "true";
                } else {
                      firstVisit = "false";
                }
          }      
            //out.print("this is the resultSet value = " + resultSet.getString(1) );
            connection.close();
    }
    catch(ClassNotFoundException cnfe) {
          System.err.println("Error loading driver: " + cnfe);
    }
    catch(SQLException sqle) {
            System.err.println("Error connecting: " + sqle);
    } catch(Exception ex) {
            System.err.println("Error with input: " + ex);
    }
   
  }

  private static void showResults(ResultSet results)
      throws SQLException {
    while(results.next()) {
      System.out.print(results.getString(1) + " ");
    }
    System.out.println();
  }

  private static void printUsage() {
    System.out.println("Usage: PreparedStatements host " +
                       "dbName username password " +
                       "vendor [print].");
  }
}            
0
benk-master-flash
Asked:
benk-master-flash
  • 4
  • 2
1 Solution
 
CEHJCommented:
>>statement.executeQuery(query);

should be

statement.executeUpdate(query);
0
 
bobbit31Commented:
are you getting a compile error? or is it just not updating?

i don't see any declaration for the variable firstVisit.
0
 
CEHJCommented:
... but unless your db is set to read uncommitted, you may not see the change as the statement does not get closed before the select
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
benk-master-flashAuthor Commented:
OK let me try this...
0
 
CEHJCommented:
You shouldn't need terminating semicolons and it would probably be better to use single quotes for character columns in statements
0
 
benk-master-flashAuthor Commented:
I should have posted the question an hour ago...
oh well. good work, nothing link a simple answer
thanks!
0
 
CEHJCommented:
8-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now