为什么使用while(rs.next())循环输出文本提示结果集没有当前行
这是我的代码用java程序编写的从数据库读取数据后保存到xml文件,用if(rs.next())就不出错,但不能循环,想用while(rs.next())实现循环,但提示...
这是我的代码 用java程序编写的从数据库读取数据后保存到xml文件,用if(rs.next())就不出错,但不能循环,想用while(rs.next())实现循环,但提示结果集没有当前行,求教大神!!!
package Ty;
import java.sql.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
public class users {
public static void main(String args[])
{
String headerstring="<?xml version=\"1.0\"?>";
String root="<users>";
String rootend="</users>";
try{
File ff=new File("user.xml");
ff.createNewFile();
FileWriter fw=new FileWriter(ff);
fw.write(headerstring);
fw.write(root);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=userdb","Jack","1234567");
Statement st=conn.createStatement();
System.out.println("驱动,连接,声明");
ResultSet rs=st.executeQuery("select * from users");
System.out.println("执行查询");
while(rs.next());
{ System.out.println("写数据");
fw.write("<users>");
fw.write("<userName>");
String userName=rs.getString("userName");
System.out.println(userName);
fw.write(userName);
fw.write("</userName>");
fw.write("<userPwd>");
String userPwd=rs.getString("userPwd");
System.out.println(userPwd);
fw.write(userPwd);
fw.write("</userPwd>");
fw.write("</users>");
}
System.out.println("执行完毕");
fw.write(rootend);
fw.close();
} catch(Exception em){System.out.println(em);}
}
} 展开
package Ty;
import java.sql.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
public class users {
public static void main(String args[])
{
String headerstring="<?xml version=\"1.0\"?>";
String root="<users>";
String rootend="</users>";
try{
File ff=new File("user.xml");
ff.createNewFile();
FileWriter fw=new FileWriter(ff);
fw.write(headerstring);
fw.write(root);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=userdb","Jack","1234567");
Statement st=conn.createStatement();
System.out.println("驱动,连接,声明");
ResultSet rs=st.executeQuery("select * from users");
System.out.println("执行查询");
while(rs.next());
{ System.out.println("写数据");
fw.write("<users>");
fw.write("<userName>");
String userName=rs.getString("userName");
System.out.println(userName);
fw.write(userName);
fw.write("</userName>");
fw.write("<userPwd>");
String userPwd=rs.getString("userPwd");
System.out.println(userPwd);
fw.write(userPwd);
fw.write("</userPwd>");
fw.write("</users>");
}
System.out.println("执行完毕");
fw.write(rootend);
fw.close();
} catch(Exception em){System.out.println(em);}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询