eclipse报An error has occurred,See error log for more details. java.lang.NullPointerException,我在网上查了一推资料还是没解决问题,各位大神有高见?下面是我的代码:UserInfoDao.java
package com.main.dao;
//查询用户名,密码。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserInfoDao {
public void login(char username,char pass) {
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/peoplepower";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "123459";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql="select * from userinfo where username='"+username+"and password='"+pass+"'";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println("用户名" + "\t" + "密码");
System.out.println("-----------------");
String us = null;
String psw = null;
while(rs.next()){
//获取stuname这列数据
us = rs.getString("username");
//获取stuid这列数据
psw = rs.getString("password");
//输出结果
System.out.println(us + "\t" + psw);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
public void main(String[] args) {
UserInfoDao dao=new UserInfoDao();
dao.login('1', '1');
}
}
在tomcat上运行后出现以下结果:
你知道web-inf目录的作用吗?只能有服务器来访问,浏览器是无法访问的,所以你直接访问web-inf目录下的东西就会报404