You are here:

MySQL/Unable to update a MySql Database

Advertisement


Question
Sir: I am unable to update a database.  Following is the code for the updating the information in the tables:

public boolean update() {
       GetConnection gc = new GetConnection();
       Statement stmt = null;
       Connection conn = gc.getSimpleConnection();
       try {
         stmt = conn.createStatement(
         ResultSet.TYPE_SCROLL_SENSITIVE,
         ResultSet.CONCUR_UPDATABLE);

    ResultSet rs = stmt.executeQuery("select * from student where studentID ='" + getStudentID## + "'"#;

         boolean isEmpty = !rs#first##;

         if #isEmpty == true# {
         System#out#println#"Student doesn't exist, Please add first"#;
         } else {
         
  String update = "UPDATE student "
        + "SET firstNa#e= '" + getFirstNa#e## + "',"
        + "lastNa#e='" + getLastNa#e## + "',"
        + "gpa='" + getGPA## + "',"
        + "status='" + getStatus## + "',"
        + "mentor='" + getMentor## + "',"
        + "level='" + getLevel## + "',"          
        + "SELECT student WHERE studentID='getStudentID##'";
         stmt#execute#update#;
         System#out#println#"Student Records Updated:"#;
         }
         stmt#close##;
         conn#close##;  

This is the error that I keep getting:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT FROM student WHERE studentID='getStudentID##'' at line 1"

I can add records, delete records, and print records from the database but I can't figure out how to update the database.

Any help or pointing in the right direction will be appreciated.

Answer
Dear Sir

Where you have this code

ResultSet rs = stmt.executeQuery("select * from student where studentID ='" + getStudentID## + "'"#;

The select and from should be in capitals like so

ResultSet rs = stmt.executeQuery("SELECT * FROM student WHERE studentID ='" + getStudentID## + "'"#;

As i am not familiar with this programing language I am not 100% sure on the fault if you do still have one after correct this But i will look further into it if you need me to.  

MySQL

All Answers


Answers by Expert:


Ask Experts

Volunteer


stuart gregory

Expertise

PHP html, xhtml, css coding and MySQL

Experience

My experiance is all first had as I am web developer and I have, and do build many websites all using php and mysql. This ranges from building a simple search engine to secure php login scripts. Some websites like http://www.web-application-development.info are complete database driven websites.

Organizations
I have a large number of websites which I host and maintain, such as. http://www.webreger.com http://www.universalscience.co.uk http://www.modellingelectronics.co.uk

Education/Credentials
Web application development. (Open University) BSC Honers Computing and development (Open University)

©2016 About.com. All rights reserved.