注册提交数据时,获取到前端的数据,但是连接数据库时报出空指针异常
【Servler】
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String rpassword = req.getParameter("rpassword");
System.out.println(username);
System.out.println(password);
System.out.println(rpassword);
UserDaoImpl userDao = new UserDaoImpl();
boolean isSuccess = userDao.Uregist(username, password, rpassword); //Uregist内出错
if (isSuccess){ // true 注册成功
// 跳转登录页面
req.getRequestDispatcher("index.jsp").forward(req,resp);
}else{
System.out.println("注册失败!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("regist.....dopost......");
this.doGet(req, resp);
}
Servlet 调用方法连接数据库时,报出后台空指针异常!
但是不使用Servlet测试数据库连接,以及对数据库操作一切正常~
下面是调用的方法。
【测试不在Servlet下连接数据库】
能正常连接数据库,并成功将数据存入数据库中
@Test
public void test(){
UserDaoImpl userDao = new UserDaoImpl();
boolean zmy = userDao.Uregist("zmy", "123", "123");
System.out.println(zmy);
}
【Uregist】
@Override
public boolean Uregist(String username,String password,String rpassword) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection(); //todo
String sql = "select username from user where username=?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
resultSet = ps.executeQuery();
if (resultSet==null || password.equals(rpassword)){
String sql2 = "insert into user (username,password) values (?,?)";
JDBCUtils.update(sql2,username,password);
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn,ps,resultSet);
}
return false;
}
【连接数据库,操作数据库】
/**
* 获取数据库连接
* @return
*/
public static Connection getConnection(){
Connection con = null;
try {
// 加载配置文件
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is); // todo NullPointerException
// 读取配置文件
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 加载驱动
Class.forName(driverClass);
con = DriverManager.getConnection(url, user, password);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
* 增删改方法
* @param sql
* @param args
*/
public static void update(String sql,Object...args){
Connection con = null;
PreparedStatement ps = null;
try {
// 获取数据库连接
con = JDBCUtils.getConnection();
// 获取PreparedStatement的实例(预编译sql语句)
ps = con.prepareStatement(sql);
// 填充占位符
for (int i=0;i<args.length;i++){
ps.setObject(i+1,args[i]);
}
// 4.执行sql语句
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closResource(con,ps);
}
}
/**
* 对数据库进行查询操作
* @param clazz
* @param sql
* @param args
* @param <T>
* @return
* @throws Exception
*/
public static <T>T getInstance(Class<T> clazz,String sql,Object...args){
Connection con = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 获取数据库连接
con = JDBCUtils.getConnection();
ps = con.prepareStatement(sql);
// 填充占位符
for (int i = 0; i < args.length; i++) {
ps.setString(i+1,(String) args[i]);
}
// 获取结果集
resultSet = ps.executeQuery();
// 获取元数据
ResultSetMetaData metaData = resultSet.getMetaData();
// 通过元数据获取列数
int columnCount = metaData.getColumnCount();
if (resultSet.next()){ // 判断是否有数据
T t = clazz.newInstance();
// 获取每一列数据
for (int i = 0; i < columnCount; i++) {
Object colValue = resultSet.getObject(i + 1);
String colName = metaData.getColumnLabel(i + 1);
// 通过反射,给student对象指定colName属性赋值
Field filed = clazz.getDeclaredField(colName);
filed.setAccessible(true);
filed.set(t,colValue);
}
return t;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(con,ps,resultSet);
}
return null;
}
【properties配置文件位置】
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties")在这里打个断点,看看是否配置文件加载了?
嗯引用properties配置信息的话,资源路径不用写.properties后缀,直接写"jdbc"试试。
删除target文件夹,重新启动一下
已解决,非常感谢各位大佬给的建议
目前没学到框架,我现在刚学完javaweb、数据库和jdbc,前后端连接现在可以不用properties文件。直接声明变量,调用就行了