qq_38069283
凌晨的独白
2017-10-24 10:57

是不是空指针的问题?是的话怎么解决

  • 指针
package login.jdbc.qr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBManagerqr {
    public static final String url = "jdbc:mysql://localhost:3306/users-info";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "qr961025";

    public Connection conn = null;
    public PreparedStatement pst = null;
    public DBManagerqr(String sql){
        try{
            Class.forName(name);
            conn =DriverManager.getConnection(url,user,password);
            pst = conn.prepareStatement(sql);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void close(){
        try{
            this.conn.close();
            this.pst.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}

 package login.dj.DAO;

import java.sql.SQLException;
import java.sql.ResultSet;

import login.jdbc.qr.DBManagerqr;

public class UserDAOqr {
    public boolean login(String userName,String password){
        boolean t=false;
        String sql = "select * from logins where name='"+userName+"'and password='"+password+"'";
        DBManagerqr db = new DBManagerqr(sql);
        try{
            ResultSet rs = db.pst.executeQuery();
            if (rs.next()) t=true;}
        catch(SQLException e){
            e.printStackTrace();
        }
        finally{
            db.close();
        }
        return t;
    }
    public static boolean existUser(String userName){
        boolean t= false;
        String sql = "select *from logins where name='"+userName+"'";
        DBManagerqr db = new DBManagerqr(sql);
        try{
            ResultSet rs = db.pst.executeQuery();
            if(rs.next()) t=true;
        }catch(SQLException e){
            e.printStackTrace();
        }
        finally{
            db.close();

        }
        return t;
    }
    public static boolean addUser(String userName,String password){
        boolean t = false;

        String sql1 = "insert into logins value('"+userName+"','"+password+"')";
        DBManagerqr db = new DBManagerqr(sql1);
        try{
            int i =db.pst.executeUpdate(sql1);
            if(i>0) t=true;
        }catch(SQLException e){
            e.printStackTrace();
        }

        return t;
    }


}

图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐

换一换