Java &引用;“结果集已关闭”;嵌套执行查询时出错
我想编写一个查询,用java打印数据库中xml标记的值Java &引用;“结果集已关闭”;嵌套执行查询时出错,java,mysql,sql,jdbc,Java,Mysql,Sql,Jdbc,我想编写一个查询,用java打印数据库中xml标记的值 <employee emp:empid=" " emp:empname="" /><location loc:locname=" "/> 使用同一语句打开rs1后,请输入这行代码 element.setAttributeNS(loc,"loc:locaname",rs.getString(1));} 将抛出异常,因为它正在处理旧的结果集(rs) 语句类状态的Javadocs: /** * <P&g
<employee emp:empid=" " emp:empname="" /><location loc:locname=" "/>
使用同一语句打开rs1后,请输入这行代码
element.setAttributeNS(loc,"loc:locaname",rs.getString(1));}
将抛出异常,因为它正在处理旧的结果集(rs)
语句类状态的Javadocs:
/**
* <P>The object used for executing a static SQL statement
* and returning the results it produces.
* <P>
* By default, only one <code>ResultSet</code> object per <code>Statement</code>
* object can be open at the same time. Therefore, if the reading of one
* <code>ResultSet</code> object is interleaved
* with the reading of another, each must have been generated by
* different <code>Statement</code> objects. All execution methods in the
* <code>Statement</code> interface implicitly close a statment's current
* <code>ResultSet</code> object if an open one exists.
*
* @see Connection#createStatement
* @see ResultSet
*/
使用同一语句打开rs1后,请输入这行代码
element.setAttributeNS(loc,"loc:locaname",rs.getString(1));}
将抛出异常,因为它正在处理旧的结果集(rs)
语句类状态的Javadocs:
/**
* <P>The object used for executing a static SQL statement
* and returning the results it produces.
* <P>
* By default, only one <code>ResultSet</code> object per <code>Statement</code>
* object can be open at the same time. Therefore, if the reading of one
* <code>ResultSet</code> object is interleaved
* with the reading of another, each must have been generated by
* different <code>Statement</code> objects. All execution methods in the
* <code>Statement</code> interface implicitly close a statment's current
* <code>ResultSet</code> object if an open one exists.
*
* @see Connection#createStatement
* @see ResultSet
*/
换句话说,我相信蝎子所说的是,你需要一个新的rs1声明。尝试添加如下新语句:
connection = dataSource.getConnection();
ResultSet rs;
connection.setAutoCommit(false);
System.out.println("Connected to server OELDBSQL!!!");
Statement stmt = connection.createStatement();
String querystring = "select empid,empname from empt";
rs = stmt.executeQuery(querystring);
Element child1 = doc.createElement("employee");
try {
while (rs.next()) {
child1.setAttributeNS(emp, "emp:empid", rs.getString(1));
child1.setAttributeNS(emp, "emp:empname", rs.getString(2));
}
String querystring1 = "select locname from Locate";
Statement stmt1 = connection.createStatement();
ResultSet rs1;
rs1 = stmt1.executeQuery(querystring1);
while (rs1.next()) {
Element element = doc.createElement("location");
child1.appendChild(element);
element.setAttributeNS(loc, "loc:locaname", rs.getString(1));
}
} catch (Exception e) {
System.out.println("Exception in connecting to DB"
+ e.getMessage());
System.err.println(e.getMessage());
}
} catch (Exception e) {
System.out
.println("Exception in connecting to DB" + e.getMessage());
System.err.println(e.getMessage());
}
我还注意到您没有关闭结果集/语句/连接。我强烈建议你关闭它们(按照与创建相反的顺序)。换句话说,我相信Scorpion所说的是你需要rs1的新声明。尝试添加如下新语句:
connection = dataSource.getConnection();
ResultSet rs;
connection.setAutoCommit(false);
System.out.println("Connected to server OELDBSQL!!!");
Statement stmt = connection.createStatement();
String querystring = "select empid,empname from empt";
rs = stmt.executeQuery(querystring);
Element child1 = doc.createElement("employee");
try {
while (rs.next()) {
child1.setAttributeNS(emp, "emp:empid", rs.getString(1));
child1.setAttributeNS(emp, "emp:empname", rs.getString(2));
}
String querystring1 = "select locname from Locate";
Statement stmt1 = connection.createStatement();
ResultSet rs1;
rs1 = stmt1.executeQuery(querystring1);
while (rs1.next()) {
Element element = doc.createElement("location");
child1.appendChild(element);
element.setAttributeNS(loc, "loc:locaname", rs.getString(1));
}
} catch (Exception e) {
System.out.println("Exception in connecting to DB"
+ e.getMessage());
System.err.println(e.getMessage());
}
} catch (Exception e) {
System.out
.println("Exception in connecting to DB" + e.getMessage());
System.err.println(e.getMessage());
}
我还注意到您没有关闭结果集/语句/连接。我强烈建议您关闭它们(以与创建相反的顺序)。我尝试了使用while循环和single resultset.querystring,但没有使用try块=“从emptlocate中选择emp.empid、emp.empname、locate.locname;然后它给出多个值意味着假设空表有5个名称。这五个名称与locname重复5次。iam获得25行,而不是5行。我尝试使用while loop with single resultset.querystring=“从Empts、locate中选择Empts.empid、Empts.empname、locate.locname;然后它给出多个值意味着假设Empts表有5个名称。这五个名称与locname重复5次。iam获得25行,而不是5行