我不知道从哪里开始我已经弄乱了我的代码,我仍然得到一个空指针异常。让我相信也许我的联系搞砸了......无论如何,不确定任何帮助都会很酷。
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();
}
}
这是我的连接类。
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;
}
}
答案 0 :(得分:1)
在createConn()
return conn
main()
,CandDLoader.createConn();
,您说
conn
因此,对返回的conn = CandDLoader.createConn();
执行任何操作并基本上将其丢弃。
将该行更改为此
private static Connection conn;
或更改
private static Connection conn = CandDLoader.createConn();
到这个
null
声明没有赋值的对象字段时,它们将被指定为private static Connection conn;
。
例如
private static Connection conn = null;
与说
完全相同@echo off
setlocal
set _project=project.vcxproj
call :do_build "%_project%" Release Win32
call :do_build "%_project%" Debug Win32
call :do_build "%_project%" Release x64
call :do_build "%_project%" Debug x64
endlocal
exit /b 0
:do_build
setlocal
set _proj=%~1
set _conf=%~2
set _arch=%~3
set _code=0
if "%_arch%"=="Win32" (set _vc_arg=x86) else (set _vc_arg=amd64)
call "%VS100COMNTOOLS%..\..\VC\vcvarsall.bat" %_vc_arg%
msbuild /t:build /p:Configuration="%_conf%" /p:Platform=%_arch% %_proj% || set "_code=1"
endlocal & exit /b %_code%
答案 1 :(得分:0)
检查代码库的以下行
conn = DriverManager.getConnection("jdbc:mysql://localhost/timeclock", "timeclockuser", "password_1234");
此处提供的网址不包含端口号,应如下所示:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/timeclock", "timeclockuser", "password_1234");
答案 2 :(得分:0)
private static Connection conn;
类中的 CallAssStatement
始终为空,您在CandDLoader.createConn();
中创建了一个连接,但实际上并未将其分配给conn
中的CallAssStatement
变量}。