欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

jsp 连接mysql空指针_java jdbc与mysql为什么连接不上并且抛出空指针异常?

发布时间:2025/3/19 数据库 29 豆豆
生活随笔 收集整理的这篇文章主要介绍了 jsp 连接mysql空指针_java jdbc与mysql为什么连接不上并且抛出空指针异常? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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()) 处报空指针

总结

以上是生活随笔为你收集整理的jsp 连接mysql空指针_java jdbc与mysql为什么连接不上并且抛出空指针异常?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。