所以我连接到数据库,我想从表投票中获取奶牛编号以显示它在条形图上,但我得到错误“java.sql.SQLException:在结果集开始之前”但我有和rs。接下来可以请一些人帮助我
conn = DbCon.ConnectDb();
String sql = "SELECT * FROM votes";
try
{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
String cows = rs.getString("cows");
if (rs.next())
{
int cowss = Integer.parseInt(cows);
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(cowss ,"Votes", "Cows");
dataset.setValue(20 ,"Votes", "Pigs");
dataset.setValue(20 ,"Votes", "Dogs");
dataset.setValue(50 ,"Votes", "Donkeys");
JFreeChart chart = ChartFactory.createBarChart("Results", "", "Votes", dataset, PlotOrientation.VERTICAL , false, true, true);
ChartFrame frame = new ChartFrame("Results", chart);
frame.setVisible(true);
frame.setSize(450,350);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
答案 0 :(得分:2)
您需要在读取列值之前调用ResultSet.next()
,以便光标指向结果中的第一行:
if (rs.next())
{
String cows = rs.getString("cows");
int cowss = Integer.parseInt(cows);
...
您在这里也不需要PreparedStatement
- 没有参数化查询。您只需使用普通Statement
。