首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

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

2012-12-15 
数据库里的表有4个字段 但当用rs.getstring(3)时程序就提示连接数据库失败我把代码贴出来String sexnew S

数据库里的表有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数据类型不对
------其他解决方案--------------------


会不会数据类型不对。代码贴全一点。还有你数据库总的有多少个字段。

热点排行