Invalid operation at current cursor position [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I made a Java Forum and im trying to search my sql table, when i click the button to finally search it (the name of the person) it comes up with a "Invalid Operation at Current Cursor Position" error.
Here'e my code for the search button. Please help me figure this out.
private void firstSearchActionPerformed(java.awt.event.ActionEvent evt) {
try{
String fname = searchText.getText();
Connection connect = DriverManager.getConnection("jdbc:derby://localhost:1527/Employees",
"users", "admin");
PreparedStatement pState = connect.prepareStatement("select * from WORKERS where First_Name = ?");
pState.setString(1,fname);
ResultSet rSet;
rSet = pState.executeQuery();
if(rs.next()){
int id_col = rSet.getInt("Employee_ID");
String id = Integer.toString(id_col);
String first = rSet.getString("First_Name");
String last = rSet.getString("Last_Name");
String job = rSet.getString("Title");
String hireDate = rSet.getString("Hire_Date");
textID.setText(id);
textFirstName.setText(first);
textLastName.setText(last);
textTitle.setText(job);
textHireDate.setText(hireDate);
}else{
JOptionPane.showMessageDialog(null, "Not in Database");
}
}catch(SQLException err){
JOptionPane.showConfirmDialog(employees.this, err.getMessage());
}
}

ResultSet rSet;
rSet = pState.executeQuery();
instead of
if(rs.next()){
use
if(rSet.next()){
Here resultset object is rSet not rs

Related

Update MySQL table row [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
Hi, I wanted to add a Password change function to my programm. For that i need to update the password column of the specific user. I researched about it and fount this and tried it:
UPDATE uprtable SET password = '"+password+"' WHERE username = '"+username+"';
I also found out that i need to select the row after it. So I did this too.
SELECT username, password FROM uprtable WHERE username '"+username+"';
The Problem is it doesnt update it and I dont get any Errors where I could get any Info about what is wrong.
Here is the Code direct from the Class:
public void changepw() throws Exception{
Connection con = getConnection();
String passwordID1 = String.valueOf((passwordfield.getPassword()));
String passwordID2 = String.valueOf((passwordfield2.getPassword()));
String passwordID3 = String.valueOf((passwordfield3.getPassword()));
if (passwordID1.equals(resultSet.getString("password"))) {
if (passwordID2.equals(passwordID3)) {
String userID = resultSet.getString("username");
try {
PreparedStatement statementpwchange = con.prepareStatement("UPDATE uprtable SET password = '"+passwordID2+"' WHERE username = '"+userID+"'");
PreparedStatement statementpwchangeconfirm = con.prepareStatement("SELECT username, password, rank FROM uprtable WHERE username = '"+userID+"'");
System.out.println("Updated");
frame.dispose();
}
catch(Exception e){
System.out.println(e);
}
}
else {
messageLabel.setForeground(Color.RED);
messageLabel.setText("The Passwords do not match");
}
}
}
If you need more Information about the Code, feel free to ask.
Thank you in advance!
You have prepared the statement but have not executed the statement. For reference: INSERT (as well as UPDATE and DELETE) statements should use executeUpdate().
SELECT statements should use executeQuery().

How I properly prepare JDBC PreparedStatment? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I started to work at my own Minecraft plugin. I need database connection to do so. I try to execute query and I get errors that I can't find solutions for.
Here is the code of function that I'm using:
public void checkIfUserExists(String login, Connection connection) {
String query = "SELECT login FROM edvault.users WHERE login = ?";
try {
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, login);
ResultSet rs = statement.executeQuery();
if (!rs.next()){
String query2 = "INSERT INTO edvault.users (login) VALUES ?";
PreparedStatement statement2 = connection.prepareStatement(query2);
statement2.setString(1 , login);
int result = statement2.executeUpdate();
if (result != 1){
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "<DBINFO> ERROR OCCURRED WHILE INSERTING NEW USER" +
" TO DATABASE");
} else {
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "<DBINFO> ADDED NEW USER TO DATABASE : LOGIN - "+
login);
}
} else
Bukkit.getConsoleSender().sendMessage("<DBINFO> USER ALREADY EXISTS IN DATABASE");
} catch (SQLException e) {
e.printStackTrace();
}
}
And here is the exception that console returns to me (this is exception for the first query, where login is xEdziu):
[22:26:21] [Server thread/WARN]: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'xEdziu' in 'where clause'
Replace
INSERT INTO edvault.users (login) VALUES ?
with
INSERT INTO edvault.users (login) VALUES (?)

JDBC COUNT() a certain row how to get the result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am very new to JDBC and I am practicing executing query's and statements.
try {
Connection conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
Statement st = conn.createStatement();
String query = "SELECT COUNT(*) AS \"Number of suppliers\", city\r\n" +
"FROM supplier\r\n" +
"GROUP BY city\r\n" +
"ORDER BY city DESC\r\n";
//st.executeUpdate(query);
ResultSet rs = st.executeQuery(query);
while(rs.next()) {
System.out.println(rs.getString("supplierNum"));
System.out.println(rs.getString("city"));
space();
}
st.close();
conn.close();
}
I am trying to execute and print the results using SELECT COUNT(supplierNum) AS "Number of suplliers", cityFROM supplierGROUP BY city ORDER BY city DESC;
The console just terminate and nothing happens no error message nothing just a blank screen.
This is the table I am trying to access
CREATE TABLE supplier
(
supplierNum CHAR(2) NOT NULL,
name CHAR(10) NOT NULL,
status TINYINT(4) NOT NULL,
city VARCHAR(10) NOT NULL,
PRIMARY KEY (supplierNum)
);
You aliased the count column as "Number of suplliers", yet your JDBC code is trying to find supplierNum. Use consistent aliases everywhere to resolve this problem:
String query = "SELECT city, COUNT(*) AS supplierCnt " +
"FROM supplier " +
"GROUP BY city " +
"ORDER BY city DESC";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getString("supplierCnt"));
System.out.println(rs.getString("city"));
space();
}

Mysql syntax error in java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
i'm pretty sure that my query is correct but i get the message " you have an error in your sql syntax; check the manual that corresponds to your MySql server version for the right syntax near to 'group='aaa' and Date_de_naissanse........." at line 1 .
i checked the syntax meany times i did it in two versions but still the same message . here's my code :
if (rdps==true){
con = connection.connect();
String sql ="SELECT * FROM eleve_ps WHERE Nom_et_prenom=? and Group=? and Date_de_naissanse=? and Responsable=? and CIN=? and Telephone=? and Adresse=?";
try{
st=con.prepareStatement(sql);
st.setString(1, txtnp.getText());
st.setString(2, txtg.getText());
st.setInt(6, Integer.parseInt(txtt.getText()));
st.setString(4, txtr.getText());
st.setString(5, txtc.getText());
st.setDate(3, date);
st.setString(7, txta.getText());
rs=st.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null,"VOTRE COURRIER DEJA EXISTE");
}else{
String insert = "INSERT INTO `eleve_ms`(`ID`, `Nom_et_prenom`, `Group`, `Date_de_naissanse`, `Responsable`, `CIN`, `Telephone`, `Adresse`) VALUES (Null,'"+txtnp.getText()+"','"+txtg.getText()+"','"+date+"','"+txtr.getText()+"','"+txtc.getText()+"','"+Integer.parseInt(txtt.getText())+"','"+txta.getText()+"')";
try{
st=con.prepareStatement(insert);
st.executeUpdate();
this.setVisible(false);
}
catch(Exception e ){
JOptionPane.showMessageDialog(null,e);
}
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
rdms=false;
}
and so is this version :
if (rdps==true){
con = connection.connect();
String sql ="SELECT * FROM eleve_ps WHERE Nom_et_prenom='"+txtnp.getText()+"' and Group='"+txtg.getText()+"' and Date_de_naissanse='"+date+"' and Responsable='"+txtr.getText()+"' and CIN='"+txtc.getText()+"' and Telephone='"+Integer.parseInt(txtt.getText())+"' and Adresse='"+txta.getText()+"' ";
try{
st=con.prepareStatement(sql);
rs=st.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null,"VOTRE COURRIER DEJA EXISTE");
}else{
String insert = "INSERT INTO `eleve_ps`(`ID`, `Nom_et_prenom`, `Group`, `Date_de_naissanse`, `Responsable`, `CIN`, `Telephone`, `Adresse`) VALUES (Null,'"+txtnp.getText()+"','"+txtg.getText()+"','"+date+"','"+txtr.getText()+"','"+txtc.getText()+"','"+Integer.parseInt(txtt.getText())+"','"+txta.getText()+"')";
try{
st=con.prepareStatement(insert);
st.executeUpdate();
this.setVisible(false);
}
catch(Exception e ){
JOptionPane.showMessageDialog(null,e);
}
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
As mentioned in comments: GROUP is a reserved word and MySQL is case insensitive.
Try this, paying attention to the back ticks surrounding the word Group.
String sql ="SELECT * FROM eleve_ps WHERE Nom_et_prenom=? and `Group`=? and Date_de_naissanse=? and Responsable=? and CIN=? and Telephone=? and Adresse=?";

i want to assign value of count query to an integer variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
`String query = "select count(book_id) as book_id from library_books_details where book_id = ?";`
PreparedStatement psEnd = collegeCon.prepareStatement(query);
psEnd.setInt(1, bookId);
ResultSet rs = psEnd.executeQuery();
if (rs.next()) {
int count= rs.getInt(1);
System.out.println("Count= " + count);
} else {
System.out.println("error: could not get the record counts");
}
as per http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html
The flow of database access is
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String strColumn = rs.getString("....");
int intColumn= rs.getInt("....");
}

Categories

Resources