这是报错
Exception in thread "main" java.lang.NullPointerException
at jdbctest.login(jdbctest.java:31)
at jdbctest.main(jdbctest.java:19)
这里是报错的行
boolean loginSuccess1 = login(userLoginInfo);//19行
String loginName=userLoginInfo.get("loginName");31行
完整代码如下
import java.sql.*; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * Created */ public class jdbctest { public static void main(String arg[]) { /**第一步 * 初始化一个界面 */ Map<String,String> userLoginInfo = initUi(); /** *验证用户名和密码 */ boolean loginSuccess1 = login(userLoginInfo); /** * 输出登录提示 */ System.out.println(loginSuccess1 ? "登录成功":"登录失败" ); } public static boolean login(Map<String, String> userLoginInfo) { //JDBC代码 String loginName=userLoginInfo.get("loginName"); String loginPwd=userLoginInfo.get("loginPwd"); //标记查询的变量 boolean loginSuccess= false; Connection conn= null; Statement stmt = null; ResultSet rs = null; /** * 第一步注册 */ try{ Class.forName("com.mysql.cj.jdbc.Driver"); /** * 第二步获取连接 */ conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest?serverTimezone=UTC", "root","password"); /** * 第三不获取数据库操作对象 */ stmt = conn.createStatement(); /** * 第四步查询说起来语句 */ String sql="select * from jdbctest where loginName = '"+loginName+"'and loginPwd='"+loginPwd+"'"; rs = stmt.executeQuery(sql); /** * 第五步处理查询结果集 */ if(rs.next()) { loginSuccess = true; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return loginSuccess; } private static Map<String, String> initUi() { Scanner s = new Scanner(System.in); System.out.println("用户名"); String loginName = s.nextLine(); System.out.println("用户密码"); String loginPwd = s.nextLine(); Map<String,String> UserLogininfo = new HashMap<>(); UserLogininfo.put("loginName",loginName); UserLogininfo.put("loginPwd",loginPwd); return null; } }