This is the java code I have tried to insert into my remote database table.
session.connect();
Channel channel = session.openChannel("exec");
String query = "INSERT INTO table_name
(id,serialno,userid,checktime,checktype,eventType)
VALUES(502,1011,0078,'2017-04-17 17:27:51',6,23)";
((ChannelExec) channel).setCommand("mysql -uuser -ppwd -h localhost -e'" +
query + "' database_name");
InputStream in = channel.getInputStream();
channel.connect();
I used the same code to execute "select *" query, instead for "Insert query" it executed perfectly and I got the output. But in this case it doesn't. Please help me find a solution.
Did you try the same command on command-line?
mysql -uuser -ppwd -h localhost -e'INSERT INTO table_name (id,serialno,userid,checktime,checktype,eventType) VALUES(502,1011,0078,'2017-04-17 17:27:51',6,23)' database_name
It would not work either. The quotes in the INSERT
command conflict with the quotes on the command-line, that wrap the SQL command.
One way is to use double-quotes on the command-line:
mysql -uuser -ppwd -h localhost -e"INSERT INTO table_name (id,serialno,userid,checktime,checktype,eventType) VALUES(502,1011,0078,'2017-04-17 17:27:51',6,23)" database_name
Then in Java, you have to escape those double quotes as \\"
, because Java uses double-quotes to delimit the string.
You Need to escape double quotes With \\"
, because double quotes are used as delimiters in Java.
String query = "INSERT INTO table_name
(id,serialno,userid,checktime,checktype,eventType)
VALUES(502,1011,0078,'2017-04-17 17:27:51',6,23)"
((ChannelExec) channel).setCommand("mysql -uroot -proot -h localhost -e\"" + query + "\" yourdbname");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.