当前位置: 代码迷 >> Eclipse >> 数据库里的表有四个字段 但当用rs.getstring(3)时程序就提示连接数据库失败
  详细解决方案

数据库里的表有四个字段 但当用rs.getstring(3)时程序就提示连接数据库失败

热度:291   发布时间:2016-04-23 02:01:02.0
数据库里的表有4个字段 但当用rs.getstring(3)时程序就提示连接数据库失败
我把代码贴出来
String sex=new String();
    String age=new String();
String id=new String();
String name=new String();

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
// TODO: handle exception
}
try {
con=DriverManager.getConnection("jdbc:odbc:redsun","","");
sql=con.createStatement();
rs=sql.executeQuery("select * FROM haoma");
while(rs.next())
{
if(haoma.equals(rs.getString(1)))
{
i++;
sex=rs.getString(3);
age=rs.getString(4);
//id=rs.getString(1);
//name=rs.getString(4);

如果不屏蔽最后两句程序运行时就提示连接数据库失败 请问是怎么回事
------最佳解决方案--------------------
那你第三个字段是什么数据类型呢,看这些代码木有发现问题,我还是贴一个JDBC的代码给你看一下吧。


Connection conn = MyDataSource.getInstance().getConnection();
String sql = "select * from t_mc where nid = ?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, nid);

rs = pstmt.executeQuery();
if (rs.next()) {
int id = rs.getInt("nid");
String sname = rs.getString("sname");
String sdescription = rs.getString("SDESCRIPTION");
double nprice = rs.getDouble("NPRICE");
String simg = rs.getString("SIMG");
String smctag = rs.getString("SMCTAG"); // 是否缺货
Date dcdate = new Date(rs.getTimestamp("DCDATE").getTime());
int nmaxid = rs.getInt("NMAXID");
int nminid = rs.getInt("NMINID");
McBean mcBean = new McBean();
mcBean.setNid(id);
mcBean.setSname(sname);
mcBean.setSdescription(sdescription);
mcBean.setNprice(nprice);
mcBean.setSimg(simg);
mcBean.setSmctag(smctag);
mcBean.setDcdate(dcdate);
mcBean.setNmaxid(nmaxid);
mcBean.setNminid(nminid);
return mcBean;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
free(rs, pstmt, conn);

}

------其他解决方案--------------------
int id = rs.getInt("nid");
                String sname = rs.getString("sname");
                String sdescription = rs.getString("SDESCRIPTION");


建议用这种方式。 
------其他解决方案--------------------
你这么说是注释了以后,现在这程序可以运行咯!那不是有getstring(3)嘛。
你把报的错误贴一下吧。
------其他解决方案--------------------
引用:
你这么说是注释了以后,现在这程序可以运行咯!那不是有getstring(3)嘛。
你把报的错误贴一下吧。

是我表述不清楚  是最多能取两个数据。当去第三个数据时就会提示连接数据库失败
比如我rs.getstring(1),getstring(2)以前后当取rs.getstring(3)就出问题了
------其他解决方案--------------------
可能是你的id数据类型不对
------其他解决方案--------------------
  相关解决方案