jdbc连接代码:
public class ConnDB{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public ConnDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
/***************************************************
*method name: executeQuery()
*功能:执行查询操作
*return value: ResultSet
* @throws ClassNotFoundException
****************************************************/
public ResultSet executeQuery(String sql) {
try{
// conn=getConnection();
// Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/database?user=root&password=root&useUnicode=true");
//JDBConnection connection=new JDBConnection();
//conn=connection.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(Exception ex){
System.err.println(ex.getMessage());
}finally{}
return rs;
}
/***************************************************
*method name: close()
*功能:关闭数据库链接
*return value: void
****************************************************/
public void close(){
try {
if (rs != null) rs.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{
try {
if (stmt != null) stmt.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
}
}
查询代码:
public int checkManager(String name,String inpwd) {
int flag = 0;
ConnDB conn=new ConnDB();
String sql = "SELECT * FROM admin where username='" +name + "'";
System.out.println(sql);
ResultSet rs = conn.executeQuery(sql);
try {
if (rs.next()) {
String pwd = inpwd;
if (pwd.equals(rs.getString(3))) {
flag = 1;
rs.last();
int rowSum = rs.getRow();
rs.first();
if (rowSum != 1) {
flag = 0;
System.out.print("获取的row的值:" + sql + rowSum);
}
} else {
flag = 0;
}
}else{
flag = 0;
}
} catch (SQLException ex) {
flag = 0;
}
// ConnDB.close(rs, st, conn);
conn.close();
return flag;
}
在 if (rs.next()) 处报空指针