这是我试图插入远程数据库表中的 java 代码。
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();
我使用相同的代码来执行“select *”查询,而不是“Insert query”,它执行得很好并且我得到了输出。但在这种情况下它没有。请帮我找到解决方案。
最佳答案
您需要使用 \"
转义双引号,因为双引号在 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");
关于java - 如何使用 channel.connect(); 对远程数据库执行 “insert into” 查询;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681093/