SQL problems with I/0 (updating)
I'm having troubles with after I input my data. When I hit the Return key after the last entry
I get this error which is pointer...
Error - 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 'name= 'lkasdj', Street= '3437', City 'Fullerton', State'CA', Zip = '3', Home Pho' at line 1.
I looked at theString updatestring =
where I think the problem is at which of theHome Pho
here is my code for the update section.
publicclass DBAssign{
/**
* @param args
*/
static Scanner kbd;
static Connection conn =null;
// searches for a emp id.
publicstaticboolean findRecord(String empid){
{
}// close finally
return retval;
}// close find
// updates the a record
publicstaticvoid updateRecord(){
// get record to update
String empid =null;
boolean findemp =false;
while (!findemp){
System.out.println("Enter Employee ID to update");
empid = kbd.next();
kbd.nextLine();
findemp = findRecord(empid);
if (!findemp){
System.out.println("This Employee id record " + empid
+" does not exist - try again");
}
}// close while
// display data and request update
Statement stmt =null;
ResultSet rs =null;
String firstname;
String lastname;
String street;
String city;
String state;
String zip;
String homephone;
String officephone;
String department;
String yearsemploy;
String monthpay;
try{
stmt = conn.createStatement();
rs = stmt.executeQuery("Select * from emptable where empid = '"
+ empid +"'");
rs.next();
System.out.println("Item First Name is "
+ rs.getString("firstname"));
System.out.print("Enter new item name: ");
firstname = kbd.next();
kbd.nextLine();
System.out.println("Item Last name is " + rs.getString("lastname"));
System.out.print("Enter new item name: ");
lastname = kbd.next();
kbd.nextLine();
System.out.println("Item Street is " + rs.getString("street"));
System.out.print("Enter new item name: ");
street = kbd.next();
kbd.nextLine();
System.out.println("Item City name is " + rs.getString("city"));
System.out.print("Enter new item name: ");
city = kbd.next();
kbd.nextLine();
System.out.println("Item State name is " + rs.getString("state"));
System.out.print("Enter new item name: ");
state = kbd.next();
kbd.nextLine();
System.out.println("Item Zip Code is " + rs.getString("zip"));
System.out.print("Enter new item name: ");
zip = kbd.next();
kbd.nextLine();
System.out.println("Item Home Phone is "
+ rs.getString("homephone"));
System.out.print("Enter new item name: ");
homephone = kbd.next();
kbd.nextLine();
System.out.println("Item Office Phone is "
+ rs.getString("officephone"));
System.out.print("Enter new item name: ");
officephone = kbd.next();
kbd.nextLine();
System.out.println("Item Department is "
+ rs.getString("department"));
System.out.print("Enter new item name: ");
department = kbd.next();
kbd.nextLine();
System.out.println("Item Years Employed is "
+ rs.getString("yearsemploy"));
System.out.print("Enter new item name: ");
yearsemploy = kbd.next();
kbd.nextLine();
System.out.println("Item Monthly pay is "
+ rs.getString("monthpay"));
System.out.print("Enter new item name: ");
monthpay = kbd.next();
kbd.nextLine();
String updatestring ="Update emptable set FirstName = '"
+ firstname +"', Last name= '" + lastname +"', Street= '"
+ street +"', City '" + city +"', State'" + state
+"', Zip = '" + zip +"', Home Phone='" + homephone
+"', Office Phone '" + officephone +"', Department '"
+ department +"', Years Employed'" + yearsemploy
+"', Month Pay '" + monthpay +"' where empid = '" + empid
+"'";
stmt.execute(updatestring);
int updatenum = stmt.getUpdateCount();
if (updatenum < 1){
System.out.println("Error on update");
}else{
System.out.println("Updated " + updatenum +" records");
}
}catch (SQLException e){
System.out.println("Error - " + e.getMessage());
}finally{
try{
if (stmt !=null){
stmt.close();
}
if (rs !=null){
rs.close();
}
}catch (SQLException e2){
System.out.println("Error " + e2.getMessage());
}
}// close finally
}// close updates
publicstaticvoid showMenu(){
System.out.println("");
System.out.println("1. List employees records");
System.out.println("2. Add employees record");
System.out.println("3. Update employees record");
System.out.println("4. Delete employees record");
System.out.println("5. Exit");
}
publicstaticint getOption(){
int optn = 99;
while (optn > 5 || optn < 1){
System.out.println("");
System.out.println("Enter option");
System.out.print("===>");
optn = kbd.nextInt();
}// close while
return optn;
}
publicstaticvoid main(String[] args){
int optn = 99;
String host ="localhost";
String database ="empdata";
String user ="root";
String pass ="";
// make keyboard object
kbd =new Scanner(System.in);
String connstring ="jdbc:mysql://" + host +"/" + database +"?user="
+ user +"&password=" + pass;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(connstring);
while (optn != 5){
showMenu();
optn = getOption();
switch (optn){
case 1:
listRecords();
break;
case 2:
addToTable();
break;
case 3:
updateRecord();
break;
case 4:
deleteRecord();
System.out.println("That Employee is deleted");
break;
case 5:
System.out.println("OK - later Come back soon");
default:
break;
}// close switch
}// close while
}catch (ClassNotFoundException e1){
System.out.println("ERROR - Class not found " + e1.getMessage());
}catch (SQLException e2){
System.out.println("ERROR - " + e2.getMessage());
System.out.println("ERROR - " + e2.getSQLState());
}catch (Exception e3){
System.out.println("Error - " + e3.getMessage());
}
}// close main
}// close class
Any help would be great.
Thanks
Red
- You aren't putting '='. City '" + city + won't work
- Are all your fields strings? Some of those fields are just numbers and should be stored in the database that way, which then means that you don't need single quotes around the values
- Prepared statements would eliminate some of these errors.
I'm not to sure if we can use Prepared statements.
I did some corrections and got rid of the headings and now I get this error message
Error - 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 ''David', 'Wie', '1', '2','3','4','5','6','7','8','9' where empid = '234DW'' at line 1
Thanks for the help so far.
red
> I'm not to sure if we can use Prepared statements.? Did your instructor say this ?
It's not written in the assignement that we can use it or not. In fact we did not even learn this...red
> It's not written in the assignement that we can use> it or not. In fact we did not even learn this...Should you take that as a "Don't use PreparedStatement. Don't showinitiative. Don't learn anything on your own" ... or not?
Sure why not and are you willing to help me if I get lost and ask dumb questions.red
If you post the error message with the relevant code, there are always forum members who can suggest something.
No such thing as a dumb question. Let's the see the revised code.
> > It's not written in the assignement that we can use
> > it or not. In fact we did not even learn this...
>
> Should you take that as a "Don't use
> PreparedStatement. Don't show
> initiative. Don't learn anything on your own" ... ornot?
I should add that using PreparedStatement is not worlds away from using
Statement, it's just a cleaner, more general and more robust approach.
I can't imagine an instructor rejecting it, but then again, I don't know your
instructor.
On the other hand, if someone used something like Hibernate that
automatically generates the SQL for inserting records, some instructors
may say that is sidestepping the goals of the assignment.
Thanks, and I know there is no sure thing as a dumb error...
this is the error I get
Error - 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 ''David', 'Wie', '1', '2','3','4','5','6','7','8','9' where empid = '234DW'' at line 1
here is my code
public static void updateRecord() {
// get record to update
String empid = null;
boolean findemp = false;
while (!findemp) {
System.out.println("Enter Employee ID to update");
empid = kbd.next();
kbd.nextLine();
findemp = findRecord(empid);
if (!findemp) {
System.out.println("This Employee id record " + empid
+ " does not exist - try again");
}
}// close while
// display data and request update
Statement stmt = null;
ResultSet rs = null;
String firstname;
String lastname;
String street;
String city;
String state;
String zip;
String homephone;
String officephone;
String department;
String yearsemploy;
String monthpay;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("Select * from emptable where empid = '"
+ empid + "'");
rs.next();
System.out.println("Item First Name is "
+ rs.getString("firstname"));
System.out.print("Enter new item name: ");
firstname = kbd.next();
kbd.nextLine();
System.out.println("Item Last name is " + rs.getString("lastname"));
System.out.print("Enter new item name: ");
lastname = kbd.next();
kbd.nextLine();
System.out.println("Item Street is " + rs.getString("street"));
System.out.print("Enter new item name: ");
street = kbd.next();
kbd.nextLine();
System.out.println("Item City name is " + rs.getString("city"));
System.out.print("Enter new item name: ");
city = kbd.next();
kbd.nextLine();
System.out.println("Item State name is " + rs.getString("state"));
System.out.print("Enter new item name: ");
state = kbd.next();
kbd.nextLine();
System.out.println("Item Zip Code is " + rs.getString("zip"));
System.out.print("Enter new item name: ");
zip = kbd.next();
kbd.nextLine();
System.out.println("Item Home Phone is "
+ rs.getString("homephone"));
System.out.print("Enter new item name: ");
homephone = kbd.next();
kbd.nextLine();
System.out.println("Item Office Phone is "
+ rs.getString("officephone"));
System.out.print("Enter new item name: ");
officephone = kbd.next();
kbd.nextLine();
System.out.println("Item Department is "
+ rs.getString("department"));
System.out.print("Enter new item name: ");
department = kbd.next();
kbd.nextLine();
System.out.println("Item Years Employed is "
+ rs.getString("yearsemploy"));
System.out.print("Enter new item name: ");
yearsemploy = kbd.next();
kbd.nextLine();
System.out.println("Item Monthly pay is "
+ rs.getString("monthpay"));
System.out.print("Enter new item name: ");
monthpay = kbd.next();
kbd.nextLine();
String updatestring = "Update emptable set FirstName ='" + empid + "', '"
+ firstname + "', '" + lastname + "', '" + street + "', '"
+ city + "','" + state + "','" + zip + "','" + homephone
+ "','" + officephone + "','" + department + "','"
+ yearsemploy + "','" + monthpay + "' where empid = '" + empid
+ "'";
stmt.execute(updatestring);
int updatenum = stmt.getUpdateCount();
if (updatenum < 1) {
System.out.println("Error on update");
} else {
System.out.println("Updated " + updatenum + " records");
}
} catch (SQLException e) {
System.out.println("Error - " + e.getMessage());
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e2) {
System.out.println("Error " + e2.getMessage());
}
}// close finally
}// close updates
Thanks
Red
Isn't the correct SQL syntax:UPDATE emptable SET FirstName ='John', LastName='Doe' WHERE empid='qwerty'Suggestion: before you execute updatestring, print it out and take a look at it. is the syntax correct?
String updatestring = "Update emptable set FirstName ='" + empid + "', '"
+ firstname + "', '" + lastname + "', '" + street + "', '"
+ city + "','" + state + "','" + zip + "','" + homephone
+ "','" + officephone + "','" + department + "','"
+ yearsemploy + "','" + monthpay + "' where empid = '" + empid
+ "'";
This is where the error occurs
Each field name being updated must be present in the statement, and each value accordingly
A smaller example is:
String updateString = "Update emptable set fname = '" + fName + "', lname = '" + lname + "'";
Apply the same to the rest of your values.
The above poster is right too. I don't think your instructor will be a pain about using PreparedStatements. I've had some a$$ professors, but none that would complain about that.
Thanks for the help
I still getting an error thou...
Error - 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 'emp = '444', pay = '555.0'' at line 1
yearsemploy should be int
and monthpay is a float
that what the db has the listed as
here is my update code.
I code has change which are in bold along with the String updateString code
public static void updateRecord() {
// get record to update
String empid = null;
boolean findemp = false;
while (!findemp) {
System.out.println("Enter Employee ID to update");
empid = kbd.next();
kbd.nextLine();
findemp = findRecord(empid);
if (!findemp) {
System.out.println("This Employee id record " + empid
+ " does not exist - try again");
}
}// close while
// display data and request update
Statement stmt = null;
ResultSet rs = null;
String firstname;
String lastname;
String street;
String city;
String state;
String zip;
String homephone;
String officephone;
String department;
[b]int yearsemploy;[/b]
[b]float monthpay;[/b]
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("Select * from emptable where empid = '"
+ empid + "'");
rs.next();
System.out.println("Item First Name is "
+ rs.getString("firstname"));
System.out.print("Enter new item name: ");
firstname = kbd.next();
System.out.println("Item Last name is " + rs.getString("lastname"));
System.out.print("Enter new item name: ");
lastname = kbd.next();
System.out.println("Item Street is " + rs.getString("street"));
System.out.print("Enter new item name: ");
street = kbd.next();
System.out.println("Item City name is " + rs.getString("city"));
System.out.print("Enter new item name: ");
city = kbd.next();
System.out.println("Item State name is " + rs.getString("state"));
System.out.print("Enter new item name: ");
state = kbd.next();
System.out.println("Item Zip Code is " + rs.getString("zip"));
System.out.print("Enter new item name: ");
zip = kbd.next();
System.out.println("Item Home Phone is "
+ rs.getString("homephone"));
System.out.print("Enter new item name: ");
homephone = kbd.next();
System.out.println("Item Office Phone is "
+ rs.getString("officephone"));
System.out.print("Enter new item name: ");
officephone = kbd.next();
System.out.println("Item Department is "
+ rs.getString("department"));
System.out.print("Enter new item name: ");
department = kbd.next();
[b]System.out.println("Item Years Employed is "
+ rs.getInt("yearsemploy"));
System.out.print("Enter new item name: ");
yearsemploy = kbd.nextInt();
System.out.println("Item Monthly pay is "
+ rs.getFloat("monthpay"));
System.out.print("Enter new item name: ");
monthpay = kbd.nextFloat();[/b]
String updateString = "Update emptable set fname = '" + firstname
+ "', lname = '" + lastname + "', street = '" + street
+ "', city = '" + city + "', state = '" + state
+ "', zip = '" + zip + "', phone = '" + homephone
+ "', office = '" + officephone + "', dept = '"
+ department + "', years emp = '" + yearsemploy
+ "', pay = '" + monthpay + "'";
stmt.execute(updateString);
int updatenum = stmt.getUpdateCount();
if (updatenum < 1) {
System.out.println("Error on update");
} else {
System.out.println("Updated " + updatenum + " records");
}
} catch (SQLException e) {
System.out.println("Error - " + e.getMessage());
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e2) {
System.out.println("Error " + e2.getMessage());
}
}// close finally
}// close updates
any helpwould be great
Thanks
Red
Message was edited by:
Redheadashley
Can you print out the updateString string in full and give it a good look?
Error messages tend to only report a portion of the string, and the
actual syntax error in the SQL may be earlier in the string.
Again, have your app print out updateString, so that you can examine the
string you are generating to see if it is what you expect it to be.
> Sure why not and are you willing to help me if I get> lost and ask dumb questions.Well since you're already asking dumb questions, what's there to lose? :)
Are emp and pay strings in your database, because now you seem to be accessing them as if they were strings (you have the single quotes). Then again emp and pay seem like integer and float, so you probably should access them without the single quotes.
Who designed the database schema? Was it you or did you get it ready made?
, years emp = '" + yearsemploy
+ "', pay = '" + monthpay + "'";
This is a portion of your updateStyring. Are years and emp two separate fields or is that one field? If it's one field, you can't have spaces between them.
If you can print out your updateString, and possibly post the table's fields it might make this faster.