我的Java代码有问题(Resultset为null)。在我尝试远程Mysql scalegrid托管之前,它一直在localhost上运行。最有趣的是,如果我使用Mysql Workbench,则查询工作正常并返回所有行。我做错了。
我尝试手动提交表;
Query (SELECT * FROM ShopDatabase.DATA)
工作正常;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.sql.*;
public class Main {
public static void main(String[] args) throws ParserConfigurationException, IOException, SQLException, ClassNotFoundException {
String userName = "App";
String password = "AppShopData+2";
String connectionUrl = "jdbc:mysql://SG-ShopDatabase-821-master.servers.mongodirector.com:3306/ShopDatabase";
try(Connection connection = DriverManager.getConnection(connectionUrl, userName, password)){
System.out.println("Connection established");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM ShopDatabase.DATA where product_name = 'Хлеб';");
while(resultSet.next()){
System.out.println(resultSet.getInt("id"));
System.out.println("----------------");
}
}
}
}
答案 0 :(得分:1)
在查询结束时删除;
:
ResultSet resultSet = statement.executeQuery(
"SELECT * FROM ShopDatabase.DATA where product_name = 'Хлеб'");
答案 1 :(得分:0)
给出正确的MySQL连接,并且确实存在一个列值“Xлеб”: