Inserting into multiple tables using a single query in preparestatement [duplicate] - java

This question already has answers here:
Can I execute multiple queries separated by semicolon with MySQL Connector/J? [duplicate]
(3 answers)
Closed 6 years ago.
This is my code
String query = "insert into Branch (CompanyID,BranchName,BranchAddress,Email,Phone,ContactPerson) values (?,?,?,?,?,?);insert into Activities(EmployeeID,ActivityType,Activities) values (?,?,?)";
// System.out.println(mobile);//BranchName | BranchAddress | Email | Phone | ContactPerson
pstmt = conn.prepareStatement(query);
pstmt.setString(1, compid);
pstmt.setString(2, name);
pstmt.setString(3, address);
pstmt.setString(4, email);
pstmt.setString(5, mobile);
pstmt.setString(6, contactperson);
pstmt.setString(7, empid);
pstmt.setString(8, acttype);
pstmt.setString(9, activity);
System.out.println(query+"-----"+acttype);
pstmt.execute();
status = true;
If I insert without dynamic values it gets inserted otherwise I get the below 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 'insert into Activities(EmployeeID,ActivityType,Activities) values ('6','createbr' at line 1
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 'insert into Activities(EmployeeID,ActivityType,Activities) values ('6','createbr' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155).........
How can I overcome this ??? I am able to insert using this statement below
insert into Branch (CompanyID,BranchName,BranchAddress,Email,Phone,ContactPerson) values ('1','sample','11 cross','fakemail#gmail.com','5467897656','Deigo');insert into Activities(EmployeeID,ActivityType,Activities) values ('6','createdbranch','cearted branch sample');
But I want it to be dynamic.Please help

allowMultiQueries=true&rewriteBatchedStatements=true needs to be added to the connection params to allow execution of multiple queries.
For example, the url may be like:
jdbc:mysql://xxx.xxx.xxx.xxx:nnnnn/db_name?allowMultiQueries=true&rewriteBatchedStatements=true&user=xxxx&password=yyyy

Related

MySQLSyntaxErrorException for a join command which is working fine on MySQL workbench

String query = "select Books.BookId, Books.Title, Book_Loans.BranchId, count(Book_Loans.BookLoanId) as NoOfCopies from (Book_Loans join Books"
+ "on (Book_Loans.BookId = Books.BookId)) where Books.BookId = ? group by (Book_Loans.BranchId)";
preparedStatement = connect.prepareStatement(query);
preparedStatement.setLong(1, bookId);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
System.out.println("BookId:"+resultSet.getLong(1)+"\nTitle:"+resultSet.getString(2)+"\nBranchId:"+resultSet.getLong(3)+"\nNoOfCopies:"+resultSet.getInt(4)+"\n");
I am trying to execute the above code snippet but I am getting MySQLSyntaxErrorException as below:
Exception in thread "main" 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 '(Book_Loans.BookId = Books.BookId)) where Books.BookId = 75 group by (Book_Loans' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
at libraryModel.CopiesCheckedOutFromEachBranchForABook.numberOfCheckedOutCopies(CopiesCheckedOutFromEachBranchForABook.java:36)
at libraryModel.Main.main(Main.java:47)
The tables related to the queries are as below:
Books(BookId, AuthorName, Title, (Publisher)Name)
Book_Loans(BookLoanId, DateOut, DueDate, BranchId, BookId, CardNo)
I am trying to display the number of copies of books checked out from each branch by asking the user to enter the branchId. The query is working fine when I ran it on MySQL workbench but giving me syntax error on Eclipse.
You are missing a space between "books" and "on". Add it and you should be fine.
The SQL query is not correct You got to add a space between Books and on. ALso, there is no need to put additional parenthesis
Use:
String query = "select Books.BookId, Books.Title, Book_Loans.BranchId, count(Book_Loans.BookLoanId) as NoOfCopies from Book_Loans join Books on Book_Loans.BookId = Books.BookId where Books.BookId = ? group by Book_Loans.BranchId";
Your query doesn't make sense. You are saying to aggregate by the branch, and then including information about books. In most databases -- and even in the more recent versions of MySQL -- this would fail.
Your query should look more like this:
select bl.BranchId, count(*) as NumCopies
from Book_Loans bl join
Books b
on bl.BookId = b.BookId
where b.BookId = ?
group by bl.BranchId;
If you want to include information about the book, then the proper way would be to use aggregation functions or to include the columns in the group by:
select bl.BranchId, count(*) as NumCopies, b.BookId, b.Title
from Book_Loans bl join
Books b
on bl.BookId = b.BookId
where b.BookId = ?
group by bl.BranchId, b.BookId, b.Title;
Notes that table aliases make a query easier to write and read.
Of course, once you get the query correct, you need to implement it correctly in your application, including getting the spacing correct around the lines of the query.

escape character for '#' -- JDBC?

I'm making a batch insert to MySQL table:
insert into table1 (field1, field2) values("aa#gmail.com", "f2 value"), ("cc#gmail.com", "another f2 here");
giving error to the character '#' in the value String:
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 'insert into buyers (field1, field2) values ('aa#' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
How can i get around this - is there some kind of escape charactr for JDBC to work this?
Note: I'm aware of JDBC-batch execution. I'm looking for a solution for the above - if any:
pStat.addBatch();
pStat.executeBatch();
TIA.
Further note: The above insert query runs fine directly on MySQL without JDBC in between. Also note: this isn't an issue when JDBC itself sets up the parameter with pStat.getString("aa#gmail.com"); -- thus the batch execn is a solution.
Try using PreparedStatement. It resolves special characters automatically and avoids sql-injection.
String queryStr = "insert into table1 (field1, field2) values(?, ?);"
try {
PreparedStatement preparedStatement = conn.prepareStatement(queryStr);
preparedStatement.setString(1, "aa#gmail.com");
preparedStatement.setString(2, "f2 value");
preparedStatement.executeUpdate();
} catch (SQLException e) {
// Error
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (conn != null) {
conn.close();
}
}
More examples: https://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/
I don't think the error message is indicating a problem with the '#' at sign character.
MySQL syntax error "right syntax to use near" usually points to the first token where the problem is encountered. In this case, it looks like MySQL is objecting to INSERT.
... near 'insert into buyers (field1, field2) values ('aa#' at line 1 at
I suspect that there is something before that insert in the SQL text, and MySQL is seeing multiple statements. That's just a guess, we're not seeing the actual code.
I recommend displaying the actual contents of the SQL text, before it's executed or prepared.
Use single quotes:
insert into table1 (field1, field2)
values('aa#gmail.com', 'f2 value'), ('cc#gmail.com', 'another f2 here');
Use the UTF-8 code for special characters when running from Java. UTF-8 code for # is \u0040:
insert into table1 (field1, field2) values("aa\u0040gmail.com", "f2 value"), ("cc\u0040gmail.com", "another f2 here");
Was doing two queries separated by ; in one.
all resolved.
nothing wrong with #.
Thanks for the insightful comments&answers.

MySQLSyntaxErrorException error in Like clause

I have this function, which gets a List of objects from MySQL, using the like clause, but when testing, returns me a error of MySQLSyntaxErrorException, what i should do?
Code
#Override
public List<Livro> getLista(String nm) {
// TODO Auto-generated method stub
List<Livro> lista = new ArrayList<Livro>();
try {
String query= "SELECT L.*, nmautor, nmeditora FROM tblivro L, tbeditora E,tbautor A "
+ "WHERE nmlivro LIKE ? and L.cdautor = A.cdautor and L.cdeditora = E.cdeditora ";
PreparedStatement stmt = conn.prepareStatement(query);
String like = "%"+nm+"%";
stmt.setString(1, like);
ResultSet rs = stmt.executeQuery(sql);
...
}}
Full Error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2504)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370)
at biblioc.dados.LivroDAO.getLista(LivroDAO.java:387)
The error is in this line:
ResultSet rs = stmt.executeQuery(sql);
Replace this with
ResultSet rs = stmt.executeQuery();
To execute a SQL string with JDBC, you only need to pass the SQL string to JDBC once, and when you are using a PreparedStatement that one time is when you prepare the statement. Once you've set all the parameters, calling executeQuery() with no arguments will run the prepared statement with the parameter values you set.
Calling the version of executeQuery that takes a single string parameter will run the SQL in that string, without allowing you to use any parameter values. This is because the PreparedStatement interface inherits this method from the Statement interface.
This behaviour of the MySQL JDBC driver is particularly confusing. You are far from being the first person to make this mistake and wonder what is going on, and I cannot believe that you will be the last. As pointed out in the comments, the JDBC driver is supposed to throw an exception when you call executeQuery with a string on a PreparedStatement. Had it done so, you may well have found a solution to your problem much quicker.
I don't know what your variable sql is. Perhaps it just contains a single letter a - I was able to reproduce your error message by running stmt.executeQuery("a").

What is reason for following sql code couldn't insert data?

My database have two column. One column is auto increment id(Primary Key) and other is 'Sentence' column. Manually type something and I can insert that value. But when I'm trying insert variable value that gives error message. I tried different ways. ('?') /( ? )/(?) Not work anything for me.
int s1=9;
String s2="Kasuni";
String sql = "INSERT INTO sentences (Sentence) VALUES ( ? )";
PreparedStatement pstmtJ = conn.prepareStatement(sql);
//pstmtJ.setInt(1, s1);
pstmtJ.setString(1,s2);
pstmtJ.executeUpdate(sql);
1st value I didn't insert because of that is auto increment value. I just comment and that shows in my above code.
Error message:
Exception in thread "main" 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 '? )' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2642)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1647)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1566)
at TestJsoup.main(TestJsoup.java:66)
When I'm tring following code:
int s1=9;
String s2="Kasuni";
String sql = "INSERT INTO sentences (Sentence) VALUES ('?')";
PreparedStatement pstmtJ = conn.prepareStatement(sql);
//pstmtJ.setInt(1, s1);
pstmtJ.setString(1,s2);
pstmtJ.executeUpdate(sql);
That gives following error messages:
Exception in thread "main" java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3646)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3630)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4481)
at TestJsoup.main(TestJsoup.java:65)
pstmtJ.executeUpdate(sql); → You're sending the original SQL string, since that is calling Statement.executeUpdate(String).
Use pstmtJ.executeUpdate(); instead (PreparedStatement.executeUpdate()) to execute the prepared statement.
Interface Statement
int executeUpdate(String sql)
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. refer java doc
public interface PreparedStatement extends Statement
int executeUpdate()
Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.refer java doc
you are calling executeUpdate(String sql) which is from Statement interface, you have to call executeUpdate() from PreparedStatement interface to resolve issue.
Modified Code :
int s1=9;
String s2="Kasuni";
String sql = "INSERT INTO sentences (Sentence) VALUES ( ? )";
PreparedStatement pstmtJ = conn.prepareStatement(sql);
//pstmtJ.setInt(1, s1);
pstmtJ.setString(1,s2);
pstmtJ.executeUpdate();

getting exception when executing prepared statement?

I am using following prepared statement :
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
String query="select ename from ? ";
st=con.prepareStatement(query);
st.setString(1,"emp");
Here i want to provide table name from UI,on above code i have hardcoded tablename value.when i try to execute this code i got the following excepion.
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 ''emp' where intensive='1000'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)
at xmlbulkinsertoperation.getConnection.<init>(getConnection.java:42)
at xmlbulkinsertoperation.getConnection.main(getConnection.java:70)
Mar 26, 2012 12:04:30 PM xmlbulkinsertoperation.getConnection <init>
i'm not able to solve this problem,please help me?
Thanks
You cannot have a prepared statement with a table name as one of the parameters. Entities (table names/db names/function names so on) cannot be parameterized in prepared statements.
You'll have to either hard code the table name or concatenate it into the query.

Categories