简体   繁体   English

无法在JDBC中执行MySQL存储过程并获得nullpointer异常

[英]Can't execute MySQL stored procedure in JDBC getting nullpointer exception

im not sure where to go from here i have messed with my code and i still get a null pointer exception. 我不确定从这里去哪里我已经弄乱了我的代码,但我仍然收到一个空指针异常。 leading me to believe that maybe my connection is messed up... anyway unsure any help would be cool. 让我相信也许我的联系混乱了……无论如何不确定任何帮助会很酷。

package callassstatement;
    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;


    public class CallAssStatement {
    private static Connection conn;


    public static void printOptions() {

    System.out.println("enter 1 to get employee");
    System.out.println("enter 2 to get department");
    System.out.println("enter 3 to exit program");

}

public static String getEmployeeMethod(String id) {
    String abc = null;
    try {

        CallableStatement cs = conn.prepareCall(" { call sp_GetEmployee(1)}");


        cs.setString(1, id);
        //register the OUT parameter before calling the stored procedure
         cs.registerOutParameter(2, java.sql.Types.VARCHAR);
        cs.registerOutParameter(3, java.sql.Types.VARCHAR);
        cs.registerOutParameter(4, java.sql.Types.VARCHAR);
        cs.registerOutParameter(5, java.sql.Types.VARCHAR);

        cs.execute();

        //read the OUT parameter now
        String employeeId = cs.getString(1);
        String lastName = cs.getString(2);
        String firstName = cs.getString(3);
        String departmentId = cs.getString(4);
        String startDate = cs.getString(5);



            abc = ("EmplyeeID: " + employeeId + " " + lastName + "," + firstName + "" + " in " 
                    + departmentId + " Department "+ ", StartDate:"+ startDate);
            return abc;

    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }
    return abc;
}

public static void main(String[] args) {

    CandDLoader.createConn();

    printOptions();
    Scanner s = new Scanner(System.in);
    Scanner id = new Scanner(System.in);

    String input = s.nextLine();


    switch (input) {
        case "1":
            System.out.println("calling get employee");
            System.out.println(" Enter employeeID:");
            String ab = id.nextLine();
            getEmployeeMethod(ab);
            break;
        case "2":
            System.out.print("calling get department");
            break;
        case "3":
            System.out.print("exiting");
            System.exit(0);
        default:
            System.out.print("what are you trying to do");
            printOptions();




    }
    }

here is my connection class. 这是我的连接课程。

package callassstatement;

   import java.sql.Connection;
   import java.sql.DriverManager;
   import java.sql.SQLException;


   public class CandDLoader {


   public static Connection createConn(){
Connection conn = null;
try{

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/timeclock", "timeclockuser", "password_1234");
    }
    catch(SQLException | ClassNotFoundException ex){}


    return conn;
    }
    }

In createConn() you do return conn , however in main() , you say createConn()您确实return conn ,但是在main() ,您说

CandDLoader.createConn(); 

Therefore doing nothing with the returned conn and essentially throwing it away. 因此,对返回的conn不执行任何操作,本质上将其丢弃。

Either change that line to this 要么将该行更改为此

conn = CandDLoader.createConn();

Or change 或改变

private static Connection conn;

to this 对此

private static Connection conn = CandDLoader.createConn();

When you declare object fields without assignment, they are assigned as null . 当您声明对象字段而未分配时,它们被分配为null

For example 例如

private static Connection conn;

Is the exact same as saying 和说的完全一样

private static Connection conn = null;

Check the below line of your codebase 检查您的代码库的以下行

conn = DriverManager.getConnection("jdbc:mysql://localhost/timeclock", "timeclockuser", "password_1234");

The url provided here does not contain the port no, it should be as below: 此处提供的url不包含端口号,应如下所示:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/timeclock", "timeclockuser", "password_1234");

private static Connection conn; in your CallAssStatement class is always null, you create a connection in CandDLoader.createConn(); CallAssStatement类中始终为null的情况下,您可以在CandDLoader.createConn();创建一个连接CandDLoader.createConn(); but you do not actually assign it to your conn variable in CallAssStatement . 但实际上并没有将其分配给CallAssStatementconn变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM