我把代码贴出来
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");
建议用这种方式。