Error message ORA-00933: SQL command not properly ended - java

I have a problem with this method:
public void insertIntoQueue(DTOQueue dtoQueue) {
DbConnection dbConnection = new DbConnection();
Connection conn;
try {
conn = dbConnection.coneccion();
String sql = " INSERT INTO PURE_ENC_QUEUE (queueId,queueName) values(?,?);";
//creating PreparedStatement object to execute query
PreparedStatement preStatement = conn.prepareStatement(sql);
preStatement.setString(1, dtoQueue.getQueueId());
preStatement.setString(2, dtoQueue.getQueueName());
int result = preStatement.executeUpdate();
DbConnection.closeConnection(conn);
if(result > 0) {
System.out.println("Insertado");
} else {
System.out.println("No insertado");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done");
}
When I run it, throws the exception
java.sql.SQLException: ORA-00933: SQL command not properly ended
Any idea about the problem? Thank you!

You need to remove the semicolon at the end of the query string, i.e.:
String sql = "INSERT INTO PURE_ENC_QUEUE (queueId,queueName) values(?,?)";
Also, it would be a good idea to refactor the code to use the try-with-resources statement.

Related

How to solve java.lang.String cannot be cast to java.sql.Date?

I want to get the selected row from my JTable and update my database with the information, which was written in the textfields (from the user).
Error message:
"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.sql.Date"
Code:
int i = tableUsers.getSelectedRow();
if(i>=0) {
try {
boolean IstWirtBoolean;
Date GeburtsdatumDate;
try {
GeburtsdatumDate = new SimpleDateFormat("yyyy-MM-dd").parse(textFieldGeburtsdatum.getText());
if (textFieldWirt.getText().equals("true")) {
IstWirtBoolean = true;
System.out.println((java.sql.Date)(model.getValueAt(i, 5)));
connect.UpdateUser((Integer.parseInt(model.getValueAt(i, 0).toString())),textFieldBenutzername.getText(), textFieldPasswort.getText(),
textFieldName.getText(), textFieldVorname.getText(), new java.sql.Date(GeburtsdatumDate.getTime()),
IstWirtBoolean);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Error at:
"new java.sql.Date(GeburtsdatumDate.getTime())"
How can I solve the error? I already transformed the String into a Date and use the java.sql.Date. What I am doing wrong?
Edit: database code:
public void UpdateUser(int ID,String Benutzername,String Passwort,String Name,String Vorname,Date Geburtsdatum,Boolean IstWirt) throws SQLException {
try {
String query = "Update user set Benutzername=?,Passwort=?,Name=?,Vorname=?,Geburtsdatum=?,IstWirt=? WHERE ID=?";
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, Benutzername);
stmt.setString(2, Passwort);
stmt.setString(3, Name);
stmt.setString(4, Vorname);
stmt.setDate(5, Geburtsdatum);
stmt.setBoolean(6, IstWirt);
stmt.setInt(7, ID);
stmt.executeUpdate();
}
catch(Exception e) {
System.out.println(e);
}
}

insert single data in data base but in data base duplicate row also added at time of insertion

I would like to insert single data in data base at a time but in data base duplicate row generate at a time.
-----------------------------
Output(in mysql table=privilege)
-----------------------------
id privilege
1 abc
2 abc
Here's how I insert the data:
public int addPrivilege(String privilege) {
PreparedStatement preparedStatement = null;
String sqlprivilege;
Connection dbConnection = null;
int pinsert = 0;
try {
sqlprivilege = "insert into privilege(privilege) values(?)";
dbConnection = ConnectionDao.getDBConnection();
preparedStatement = dbConnection.prepareStatement(sqlprivilege);
preparedStatement.setString(1, privilege);
pinsert = preparedStatement.executeUpdate();
if(preparedStatement.executeUpdate()==1)
pinsert=1;
else
pinsert=0;
System.out.println("privilege is add and name is:- " +privilege);
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dbConnection != null) {
try {
dbConnection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return pinsert;
}
You execute the sql twice in your code.
1) pinsert = preparedStatement.executeUpdate();
2) if(preparedStatement.executeUpdate()==1)
I'm not sure about the return values of the executeUpdate. But i think the first is enough, no need to check for the return values. If you need to then compare with pinsert instead of executeupdate (again).
if(pinsert == 1)
You are using two time executeUpdate(); delete execute which one is in if case
public int addPrivilege(String privilege) {
PreparedStatement preparedStatement = null;
String sqlprivilege;
Connection dbConnection = null;
int pinsert = 0;
try {
sqlprivilege = "insert into privilege(privilege) values(?)";
dbConnection = ConnectionDao.getDBConnection();
preparedStatement = dbConnection.prepareStatement(sqlprivilege);
preparedStatement.setString(1, privilege);
pinsert = preparedStatement.executeUpdate();
//try this
if(pinsert ==1)
pinsert=1;
else
pinsert=0;
System.out.println("privilege is add and name is:- " +privilege);
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dbConnection != null) {
try {
dbConnection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return pinsert;
}

Inserting into MySQL database using Prepared

I am using a PreparedStatement to insert into mysql,I get no error but data has not been inserted,when I check in the mysql console it says Empty set:
public void insertGeometryValues(String gisuniqkey,String objkey,String objtype,String geometry)
{
PreparedStatement statement=null;
String sql="Insert into ZMAPERP_GIS_DB (GISUNIQKEY, GEOMETRY,OBJTYPE,OBJKEY) values(?,?,?,?);";
try {
conn.setAutoCommit(false);
statement=(PreparedStatement) conn.prepareStatement(sql);
statement.setString(1, gisuniqkey);
statement.setString(2,geometry);
statement.setString(3,objtype);
statement.setString(4,objkey);
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I also tried using a Statement like this and get this ERROR
try {
if(conn==null)
{
System.out.println("The connection was not initialized.");
return false;
}
Statement st=(Statement) conn.createStatement();
String sql="Insert into ZMAPERP_GIS_DB (GISUNIQKEY, GEOMETRY,OBJTYPE,OBJKEY) values('"+gisuniqkey+"','"+geometry+"','"+objtype+"',"+objkey+"');";
System.out.println(sql);
rc=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Your use of PreparedStatement never actually executes the statement. Change your code to:
public void insertGeometryValues(String gisuniqkey,String objkey,String objtype,String geometry) {
String sql="Insert into ZMAPERP_GIS_DB (GISUNIQKEY, GEOMETRY,OBJTYPE,OBJKEY) values(?,?,?,?);";
conn.setAutoCommit(false);
try (PreparedStatement statement = conn.prepareStatement(sql)) {
statement.setString(1, gisuniqkey);
statement.setString(2,geometry);
statement.setString(3,objtype);
statement.setString(4,objkey);
statement.executeUpdate(); // <---- This is what is missing!
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have also included try-with-resources so that the statement is actually correctly closed.
the answer given by Mark is correct. that should help you inserting using prepared statement.
You Sql query for inserting using Statement is incorrect. you have missed the starting ' for objkey variable. the correct query is
String sql="Insert into ZMAPERP_GIS_DB (GISUNIQKEY, GEOMETRY,OBJTYPE,OBJKEY) values('"+gisuniqkey+"','"+geometry+"','"+objtype+"','"+objkey+"');";

Why i can't connect to mysql database in java?

I found this code in a tutorial but it isn't working when I debug it try the connection and then it isn't throw exception only go to the finally block without do any line of code under the DriverManager.getConnection().
Why? Anyone has an idea?
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://host/databasename";
String user = "user";
String password = "pass";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
con = (Connection) DriverManager.getConnection(url, user, password);
st = (Statement) con.createStatement();
rs = st.executeQuery("SELECT * FROM Message");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Logcat write this:
Logcat Message
1) DriverManager.getConnection() returns an object of type Connection, so there is no need to cast it. Same thing for st = (Statement) con.createStatement();
2) Unless you're running MySQL on a remote machine, you need to make sure you have MySQL installed on your local machine. If you decide to run MySQL on your local machine, you can connect to it with String url = "jdbc:mysql://localhost/{existing_db_name}"; given that everything else remains the same.
3) It seems like you're catching the same exception twice:
try{
con = (Connection) DriverManager.getConnection(url, user, password);
...
}catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
As a result, your second catch block, the one that prints the exception message to System.err, never runs. Instead, you print the exception message to a Logger. This might be why you think no exception is being thrown, when in reality, an exception is being thrown.
4) Make sure you download the JDBC driver for MySQL. Copy and paste it into the directory of your project.
I've deleted some catch block:
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://host:3306/databasename";
String user = "user";
String password = "pass";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
con = (Connection) DriverManager.getConnection(url, user, password);
st = (Statement) con.createStatement();
rs = st.executeQuery("SELECT * FROM Message");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
ex.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
Can you please specify what String url are you using? At this point, with these modifications I obviously get a SQLException with No suitable driver found for jdbc:mysql://host/databasename

SQLite out of memory exception

I am getting the "java.sql.SQLException: [SQLITE_MISUSE] Library used incorrectly (out of memory)". I paste the code sample for my database connection object
public class DBhandler {
private String DBUrl="d:\\sqlitedb\\somdb.db";
private String driverName="org.sqlite.JDBC";
private String jdbc="jdbc:sqlite:";
private Connection con=null;
private Statement stmnt=null;
public DBhandler()
{
try {
Class.forName(this.driverName);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
this.con=DriverManager.getConnection(this.jdbc+this.DBUrl);
this.stmnt=con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public CurrentActiveSom getCurrentActiveSom()
{
CurrentActiveSom cas=null;
String query="select * from current_active_som where active=1";
ResultSet rs=null;
try {
rs=this.stmnt.executeQuery(query);
if (rs.next())
{
cas= new CurrentActiveSom();
cas.setMonth(rs.getString("month"));
cas.setYear(rs.getString("year"));
cas.setIsActive(rs.getInt("active"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
rs.close();
this.stmnt.close();
this.con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return cas;
}
public CurrentActiveSom getIsDoneSom(String month,String year)
{
CurrentActiveSom cas=null;
String query="select * from current_active_som where month='"+month+"' and year='"+year+"' and active=0";
ResultSet rs=null;
try {
rs=this.stmnt.executeQuery(query); //this is exception line
}
if (rs.next())
{
cas= new CurrentActiveSom();
cas.setMonth(rs.getString("month"));
cas.setYear(rs.getString("year"));
cas.setIsActive(rs.getInt("active"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
//rs.close(); //if i uncomment this gets null pointer exception
this.stmnt.close();
this.con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return cas;
}
The call to these Two methods with same object of DBhandler like
DBhandler db=new DBhandler();
CurrentActiveSom cas1=db.getCurrentActiveSom();
CurrentActiveSom cas2=db.getIsDoneSom(String month,String year)
then I am getting the above exception ,
but if we call thses 2 methods with different object DBhandler like
DBhandler db1=new DBhandler();
DBhandler db2=new DBhandler();
CurrentActiveSom cas1=db1.getCurrentActiveSom();
CurrentActiveSom cas2=db2.getIsDoneSom(String month,String year)
Then code is working fine.
Is this because of sync problem, with connection ? how to resolve this problem?
Well, the "out of memory"error looks weird, but one definitive error lies in creating Connection once per program run (in the constructor) and then closing it in each data access method.
This code:
CurrentActiveSom cas1=db.getCurrentActiveSom();
closes the Connection, so this code:
CurrentActiveSom cas2=db.getIsDoneSom(String month,String year)
tries to get data from a closed database. This is OK if you are using some kind of connection pooling in which closing a connection returns it to the pool. But it seems you're working on a single physical connection.
So just close it after you're done getting data from DB, and not in each data access method.
You close connection before you call `rs.next()so ResultSet try to read from connection, that has been already closed.

Categories