Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
How to execute an “insert into” query to a remote database with channel.connect();

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!