mysql连接两个表返回正确的数据,但是连接三个表使用java语句executeQuery返回空

时间:2018-12-11 06:13:05

标签: java mysql jdbc

我有三个表:

integer -> integer.toString()

前两个表联接查询代码

table req_specs(id int,testproject_id int,doc_id varchar) 

table requirements(id int,srs_id int,req_doc_id varchar)

tables nodes_hierarchy(id int,name varchar,parent_id int,node_type_id int,node_order int)

public String getRequirementDocIDofProject(String testprojectName) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException { String req_doc_id = ""; initDB(); resultSet = statement.executeQuery("select * from req_specs " + "INNER JOIN requirements reqs " + "on req_specs.id =reqs.srs_id where req_specs.testproject_id=7165 "); while (resultSet.next()) { req_doc_id = req_doc_id+" "+resultSet.getString("req_doc_id"); } close(); return req_doc_id; } 返回正确的数据

后三个表联接查询代码

dbcon.getRequirementDocIDofProject("XXX");

public String getRequirementDocIDofProject(String testprojectName) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException { String req_doc_id = ""; initDB(); resultSet = statement.executeQuery("select * from req_specs INNER JOIN nodes_hierarchy nh " + "on nh.id=req_specs.testproject_id " + "INNER JOIN requirements reqs " + "on req_specs.id =reqs.srs_id where nh.name='"+testprojectName+"' "); while (resultSet.next()) { req_doc_id = req_doc_id+" "+resultSet.getString("req_doc_id"); } close(); return req_doc_id; } 返回空。

但是当我将mysql放入navicat时,它会显示正确的值

dbcon.getRequirementDocIDofProject("XXX");

1 个答案:

答案 0 :(得分:-1)

我用代码找到了答案

 "on req_specs.id =reqs.srs_id where nh.name='"+testprojectName+"'   ");

字段nh.name的值不是英文字符。我将指定正确的字符编码。

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8","username", "password");