主函数:
package test;
import javabean.Type;
import db_connoper.DB_Type;
public class Test_DBType {
public static void main(String[] args) {
DB_Type db_type = new DB_Type();
Type type = db_type.getType(2);
System.out.println(type.getTypename());
}
}
从方法:
package db_connoper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javabean.Type;
public class DB_Type {
Connection conn = null;
PreparedStatement ptmt = null;
ResultSet rs = null;
Type type = null;
public Type getType(int id){
try {
conn = DB_Conn.getConnection();//链接数据库的方法,没问题的
String sql = "select typeid,typename,typedesc from type where typeid =?";
ptmt = conn.prepareStatement(sql);
ptmt.setInt(1, id);
rs = ptmt.executeQuery();
if(rs.next()){ //该行总是报空指针的错误
type.setTypeid(id);
type.setTypename(rs.getString("typename"));
type.setTypedesc(rs.getString("typedesc"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB_Conn.free(rs, ptmt, conn);//关闭数据库资源
}
return type;
}
}
数据库type表中有字段:typeid,typename,typedesc
已存在javabean:Type,提供的是以上三个字段的setter与getter方法
错误:空指针的错误。
|
-
1.jpg
(15.89 KB, 下载次数: 27)
|