Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda