Related
public static int updateApproved(admin u){
int status=0;
try{
Connection con=getConnection();
PreparedStatement ps=con.prepareStatement("update admission set status=? where admiss_id=?");
ps.setString(1,u.getStatus());
ps.setInt(2,u.getAdmiss_id());
status=ps.executeUpdate();
PreparedStatement ps2=con.prepareStatement("insert into patient(username,password,email,sex,level,fullname,age,bday,blood,address,vaccines,fam_his,surgery,medicine_taken) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,u.getUsername());
ps.setString(2,u.getPassword());
ps.setString(3,u.getEmail());
ps.setString(4,u.getSex());
ps.setInt(5,u.getLevel());
ps.setString(6,u.getFullname());
ps.setInt(7,u.getAge());
ps.setString(8,u.getBday());
ps.setString(9,u.getAddress());
ps.setString(10,u.getBlood());
ps.setString(11,u.getVaccines());
ps.setString(12,u.getFam_his());
ps.setString(13,u.getSurgery());
ps.setString(14,u.getMedicine_taken());
status=ps2.executeUpdate();
}catch(Exception e){System.out.println(e);}
return status;
}
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). Why is this always the error? i have counter the ranges of the parameter, but i still get that error.
PreparedStatement ps2=con.prepareStatement("insert into patient(username,password,email,sex,level,fullname,age,bday,blood,address,vaccines,fam_his,surgery,medicine_taken) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,u.getUsername()); // ps has this
ps.setString(2,u.getPassword()); // ps has this
ps.setString(3,u.getEmail()); // ps does not have this, it only has 2 ?'s in it, so it explodes
You're making PreparedStatement ps2 but your setStrings are all on ps... You need to update those to use ps2
The problem is you have misused the variables ps and ps2.
As you have created a PreparedStatement variable above, you can use it again without creating a new one.
ps = con.prepareStatement("insert into patient
(username, password, email, sex,
level, fullname, age, bday, blood, address, vaccines, fam_his, surgery, medicine_taken)
values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
I've researched this error, and it seems like it comes from mere mistakes on behalf of the SQL command. My command looks right to me, but maybe I'm missing something. My full error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'VALUES ('John', 'Doe', '12/19/91', 'N/A', 'Single', 'N/A', 'N/A', 'N/A', '12/10/' at line 1
Now my code snippet, any help is greatly appreciated:
String DML = "INSERT INTO MEMBERS (FIRST_NAME, LAST_NAME, BIRTHDATE, DEATH_DATE, MARITAL_STATUS, WEDDING_DATE, SPOUSE_NAME, MILITARY_SERVICE, DATE_JOINED, DEPARTURE_DATE, ACCEPTANCE_MODE, DEPARTURE_MODE, RELATED_TO, NOTES VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(DML);
pstmt.setString(1, jTextField2.getText());
pstmt.setString(2, jTextField1.getText());
pstmt.setString(3, jTextField6.getText());
pstmt.setString(4, jTextField11.getText());
pstmt.setString(5, jTextField3.getText());
pstmt.setString(6, jTextField5.getText());
pstmt.setString(7, jTextField4.getText());
pstmt.setString(8, jTextField8.getText());
pstmt.setString(9, jTextField7.getText());
pstmt.setString(10, jTextField10.getText());
pstmt.setString(11, jTextField9.getText());
pstmt.setString(12, jTextField13.getText());
pstmt.setString(13, jTextField14.getText());
pstmt.setString(14, jTextArea1.getText());
pstmt.executeUpdate();
Missing ) between NOTES) VALUES
INSERT INTO MEMBERS (FIRST_NAME, LAST_NAME, BIRTHDATE, DEATH_DATE, MARITAL_STATUS, WEDDING_DATE, SPOUSE_NAME, MILITARY_SERVICE, DATE_JOINED, DEPARTURE_DATE, ACCEPTANCE_MODE, DEPARTURE_MODE, RELATED_TO, NOTES) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
I have a problem with PreparedStatement.
This is my function that adds new user to MySQL database:
public static void createUser(String fn, String sn, String log, String pass, int accNum, String qst, String answ) {
try {
Connection conn = (Connection) mySQLConnector.getConnection();
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(
"INSERT INTO users"
+ "(FirstName, LastName, Login, Password, AccountNumber, Ballance, Question, Answer)"
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?");
ps.setString(1, fn);
ps.setString(2, sn);
ps.setString(3, log);
ps.setString(4, pass);
ps.setInt(5, accNum);
ps.setDouble(6, 0);
ps.setString(7, qst);
ps.setString(8, answ);
ps.executeUpdate();
ps.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
And this is an error that I get:
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 '' at line 1
I have no idea what is wrong with my query. ColumnNames are ok, function arguments are also ok.
I've tried adding '' to columns names (like that: 'FirstName') but i t still doesn't work...
EDIT:
Adding spaces did not helped.I even made it in one line:
"INSERT INTO users (FirstName, LastName, Login, Password,
AccountNumber, Ballance, Question, Answer) VALUES (?, ?, ?, ?, ?, ?,
?, ?)"
and still gives the same error
You are missing spaces. Change your SQL to :
"INSERT INTO users " // space added
+ "(FirstName, LastName, Login, Password, AccountNumber, Ballance, Question, Answer) " // space added
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
EDIT : I missed the missing closing bracket at the end of the VALUES clause.
I think you just need to add spaces and bracket
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(
"INSERT INTO users "
+ "(FirstName, LastName, Login, Password, AccountNumber, Ballance, Question, Answer) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
Fix this:
"VALUES (?, ?, ?, ?, ?, ?, ?, ?");
to this:
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
I get this error when im making a new character to my game, in the CreateCharHandler it sends "saveToDb(false);" but when im ingame with another char i manually created i can saveToDb(true); with no error. please help, why is this happening?
http://i56.tinypic.com/oh1pn5.png
SaveToDb method http://pastebin.com/9sT5XBxp
line 3514 is:
ResultSet rs = ps.getGeneratedKeys();
Thanks in advance!
Your SQLException clearly states that:
You need to specify Statement.RETURN_GENERATED_KEYS to the
Statement.executeUpdate() or Connection.prepareStatement().
This can be achieved as follows (adding an additional value on Connection.prepareStatement() method):
String SQL = ""; //whatever my String is
PreparedStatement ps = connection.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, "value");
//Other necessary ps.setXXX() methods
//now update
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
The Statement.RETURN_GENERATED_KEYS is key here.
Hope this helps!
PS: Useful resource.
#Charlie berg, since you prefer being lazy, I changed line 13 of your code to include the Statement.RETURN_GENERATED_KEYS:
ps = con.prepareStatement("INSERT INTO characters (level, fame, str, dex, luk, `int`, exp, hp, mp, maxhp, maxmp, sp, ap, gm, skincolor, gender, job, hair, face, map, meso, hpMpUsed, spawnpoint, party, buddyCapacity, messengerid, messengerposition, mountlevel, mounttiredness, mountexp, equipslots, useslots, setupslots, etcslots, monsterbookcover, watchedcygnusintro, vanquisherStage, dojopoints, lastDojoStage, finishedDojoTutorial, vanquisherKills, matchcardwins, matchcardlosses, matchcardties, omokwins, omoklosses, omokties, givenRiceCakes, partyquestitems, jailtime, accountid, name, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
Also, Statement class is of package java.sql (make sure you import correctly). :-)
Oracle Documents:
If there is no indication that auto-generated columns should be made
available for retrieval, a call to Statement.getGeneratedKeys will
return a null ResultSet.
You should explicitly tell to JDBC That you want generated keys.
like this:
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
or
conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
and then you can use getGeneratedKeys().
I have this code:
Date start = new Date(Integer.parseInt(jTextField4.getText()), Integer.parseInt(jTextField16.getText()), Integer.parseInt(jTextField17.getText()));
Date end = new Date(Integer.parseInt(jTextField5.getText()), Integer.parseInt(jTextField18.getText()), Integer.parseInt(jTextField19.getText()));
statement = connection.createStatement();
preparedStatement1 = connection.prepareStatement("insert into sportmangg(customer_code,"
+ "sportman_code, start, finish, salary,amount,box salary,private salary, food salary, "
+ "other salary, bime salary, number) "
+ "values (? ,?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?");
preparedStatement1.setString(1,jTextField15.getText());
preparedStatement1.setString(2, jTextField1.getText());
preparedStatement1.setDate(3, start);
preparedStatement1.setDate(4, end);
preparedStatement1.setInt(5, Integer.parseInt(jTextField6.getText()) );
preparedStatement1.setInt(6,Integer.parseInt(jTextField14.getText()) );
preparedStatement1.setInt(7, Integer.parseInt(jTextField7.getText()));
preparedStatement1.setInt(8, Integer.parseInt(jTextField8.getText()));
preparedStatement1.setInt(9, Integer.parseInt(jTextField9.getText()));
preparedStatement1.setInt(10, Integer.parseInt(jTextField11.getText()));
preparedStatement1.setInt(11, Integer.parseInt(jTextField10.getText()));
preparedStatement1.setInt(12, Integer.parseInt(jTextField20.getText()));
preparedStatement1.executeUpdate();
but it has this 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 'salary,private salary, food salary, other salary, bime salary, number) values ('' at line 1
What is the problem?
You really shouldn't have spaces in the field name. Try surrounding it with ``
Column names with spaces in them are a very bad idea.
If you must have them, surround them with backticks:
`private salary`
You missed ) in the last line of your SQL query so it should be:
+ " values (? ,?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ? )";
Maybe you can try this:
https://github.com/stuparmihailo/util4j/releases/tag/v1.0
It's some simple project and has nice way for creating statements:
String query = "INSERT INTO table VALUES (?,?,?,?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(query);
StatementUtil.fill(stmt, 45, "text", 2, null, new Date(), false, 3.5);
You should replace private salary with private_salary and keep working with acceptable column name conventions.
column or table names should not have spaces. Join them by underscore. and make them upper case... these are not rules but accepted ways of working with DB objects. If names cannot be changed in the DB and you are stuck with something like some salary, then some salary should help.
mehdi;
I think what you have to do is all of this:
change names of
space-named columns (private salary,
food salary, other salary, bime
salary) either by replacing spaces by underscores
(recommended by naming conventions) or by
surrounding names with grave accent char:
`box salary`, `private salary`, `food salary`, `other salary`, `bime
salary`
Fix this line adding final parentheses
+ "values (? ,?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?");
it must say:
+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
Finally I'd recommend to take out prepareStatement argument to a String or StringBuffer variable, say "sqlString" or something, so you can manipulate it more transparently. Something like this:
String sqlString = "";
sqlString += " insert into sportmangg";
sqlString += " (customer_code, sportman_code, start, finish,";
sqlString += " salary, amount, box_salary, private_salary,";
sqlString += " food_salary, other_salary, bime_salary, number)";
sqlString += " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
preparedStatement1 = connection.prepareStatement(sqlString);
(or if you use StringBuffer use append method)