【发布时间】:2011-01-16 15:55:40
【问题描述】:
try
{
PreparedStatement s = (PreparedStatement) conn.prepareStatement("SELECT voters.Check,count(*) FROM voting.voters where FirstName="+first+"and LastName="+last+" and SSN="+voter_ID);
//java.sql.Statement k = conn.createStatement();
rs=s.executeQuery();
//s.executeQuery("SELECT voters.Check,count(*) FROM voting.voters where FirstName="+first+"and LastName="+last+" and SSN="+voter_ID);
System.out.println(rs.first());
c=rs.getInt(1);
d=rs.getInt(2);
System.out.println(c);
System.out.println(d);
if(c==1 && d==1)
{
s.executeUpdate("update cand set total=total+1 where ssn="+can_ID);
System.out.println("Succeful vote");
System.out.println("after vote");
s.executeUpdate("update voters set voters.Check=1 where ssn="+voter_ID);
toclient=1;
PreparedStatement qw = (PreparedStatement) conn.prepareStatement("select FirstName from cand where ssn="+can_ID);
// rs=k.executeQuery("select FirstName from cand where ssn="+can_ID);
rs1 = qw.executeQuery();//Error Here Plz help me
String name1= (String) rs1.getString(1);
System.out.println(name1);
s.executeUpdate("update voters set VTO="+name1+"where ssn="+voter_ID);
System.out.println(rs.getString(1));
}
else
{
if(c != -1)
toclient =2;
if( d ==0)
toclient =3;
if( d>1)
toclient =4;
}
System.out.println("out-----------");
rs.close();
s.close();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
错误是:
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1072)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:986)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:981)
【问题讨论】:
-
恕我直言,在没有任何解释的情况下发布代码块和一些异常并不是很好:/
-
我更喜欢这个而不是“我有问题,请帮助”并且没有任何代码;)。而且这个很简单,所以不用那么麻烦。
-
我明白你的意思,但我看不出一点解释的危害。我想有一个妥协。
-
没有利用preparedstatement的权力(查询中的硬编码值),没有利用SQL的权力(所有不必要的
SELECT查询),糟糕的SQL(count(*)没有意义),资源泄漏(finally没有关闭),糟糕的代码风格(不完全清楚你想要实现的目标),糟糕的数据模型(不明确的列名,显然没有 FK 关系(使用名字而不是 ID))。我想帮你解决这一切,但我不知道从哪里开始。