恼人的 JDBC - MySQL 错误“createStatment 调用中的致命异常”,如何解决这个问题?
Annoying JDBC - MySQL error " FATAL EXCEPTION in createStatment call", How to solve this?
当我 运行 模拟器时,它运行完美,没有任何错误,但是当我按下登录按钮时,出现错误 "java.lang.NullPointerException: Attempt to invoke interface method"。
这是我的代码:
public class MainActivity extends ActionBarActivity {
Button add;
TextView errorlbl;
EditText name, address, pincode;
Connection connect;
PreparedStatement preparedStatement;
Statement st;
String ipaddress, db, username, password;
@SuppressLint("NewApi")
private Connection ConnectionHelper(String user, String password,
String database, String server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
+ "databaseName=" + database + ";user=" + user
+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return connection;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.btnadd);
errorlbl = (TextView) findViewById(R.id.lblerror);
name = (EditText) findViewById(R.id.txtname);
address = (EditText) findViewById(R.id.txtaddress);
pincode = (EditText) findViewById(R.id.txtpincode);
ipaddress = "92.244.93.250:3306";
db = "lokal";
username = "lokal";
password = "12lokal34";
connect = ConnectionHelper(username, password, db, ipaddress);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
st = connect.createStatement();
preparedStatement = connect
.prepareStatement("insert into studentRecord(Name,Address,Pincode) values ('"
+ name.getText().toString()
+ "','"
+ address.getText().toString()
+ "','"
+ pincode.getText().toString() + "')");
preparedStatement.executeUpdate();
errorlbl.setText("Data Added successfully");
} catch (SQLException e) {
errorlbl.setText(e.getMessage().toString());
}
}
});
}
}
Logcat 错误说:
2182-2182/app.mysqlapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: app.mysqlapp, PID: 2182
java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
您的 ConnectionHelper
方法会捕获异常,而当出现问题时 returns null
。因此,在调用它时,由于您已选择退出结构化异常处理,因此您必须允许 null
return 值。你没有这样做,你只是在使用 ConnectionHelper
的 return 值,而不先检查它是否是 null
。
显然,ConnectionHelper
中发生错误,您正在将其捕获并转储到日志中。所以这会告诉你出了什么问题。
旁注:Java 中的压倒性 约定是不 将方法的第一个字母大写。
旁注 2:更好的做法 允许 ConnectionHelper
中的异常传播到您的顶级入口点并在那里处理它们。
很明显,您的连接对象为 null,这意味着 ConnectionHelper() 正在返回 null。这进一步意味着下面的行正在返回 null
connection = DriverManager.getConnection(ConnectionURL);
现在只有当驱动程序名称错误或 ConnectionURL 错误时才会发生这种情况。由于驱动程序名称似乎正确,唯一错误的地方是您的连接 url 字符串。我刚刚在 SO.
上检查了这个类似的答案 here
对我来说,连接 URl 字符串应该是
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
+ "database=" + database + ";user=" + user
+ ";password=" + password + ";";
注意连接字符串中 database
和 databaseName
变量的区别。这应该有效。
更新@T.J中提到的所有要点。 Crowder的回答是正确的,你应该养成遵循标准约定的习惯。
从您的代码中可以清楚地看出,您的 ConnectionHelper 中出现了问题,它返回 null
注意:这只是我的猜测
您使用了 net.sourceforge.jtds.jdbc.Driver,那是用于 MsSql 服务器的,并且您已将 3306 作为数据库服务器的端口,大部分 3306 用于 MySql,所以您确定已在 3306
上配置您的 MsSql
当我 运行 模拟器时,它运行完美,没有任何错误,但是当我按下登录按钮时,出现错误 "java.lang.NullPointerException: Attempt to invoke interface method"。
这是我的代码:
public class MainActivity extends ActionBarActivity {
Button add;
TextView errorlbl;
EditText name, address, pincode;
Connection connect;
PreparedStatement preparedStatement;
Statement st;
String ipaddress, db, username, password;
@SuppressLint("NewApi")
private Connection ConnectionHelper(String user, String password,
String database, String server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
+ "databaseName=" + database + ";user=" + user
+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return connection;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.btnadd);
errorlbl = (TextView) findViewById(R.id.lblerror);
name = (EditText) findViewById(R.id.txtname);
address = (EditText) findViewById(R.id.txtaddress);
pincode = (EditText) findViewById(R.id.txtpincode);
ipaddress = "92.244.93.250:3306";
db = "lokal";
username = "lokal";
password = "12lokal34";
connect = ConnectionHelper(username, password, db, ipaddress);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
st = connect.createStatement();
preparedStatement = connect
.prepareStatement("insert into studentRecord(Name,Address,Pincode) values ('"
+ name.getText().toString()
+ "','"
+ address.getText().toString()
+ "','"
+ pincode.getText().toString() + "')");
preparedStatement.executeUpdate();
errorlbl.setText("Data Added successfully");
} catch (SQLException e) {
errorlbl.setText(e.getMessage().toString());
}
}
});
}
}
Logcat 错误说:
2182-2182/app.mysqlapp E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: app.mysqlapp, PID: 2182 java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
您的 ConnectionHelper
方法会捕获异常,而当出现问题时 returns null
。因此,在调用它时,由于您已选择退出结构化异常处理,因此您必须允许 null
return 值。你没有这样做,你只是在使用 ConnectionHelper
的 return 值,而不先检查它是否是 null
。
显然,ConnectionHelper
中发生错误,您正在将其捕获并转储到日志中。所以这会告诉你出了什么问题。
旁注:Java 中的压倒性 约定是不 将方法的第一个字母大写。
旁注 2:更好的做法 允许 ConnectionHelper
中的异常传播到您的顶级入口点并在那里处理它们。
很明显,您的连接对象为 null,这意味着 ConnectionHelper() 正在返回 null。这进一步意味着下面的行正在返回 null
connection = DriverManager.getConnection(ConnectionURL);
现在只有当驱动程序名称错误或 ConnectionURL 错误时才会发生这种情况。由于驱动程序名称似乎正确,唯一错误的地方是您的连接 url 字符串。我刚刚在 SO.
上检查了这个类似的答案 here对我来说,连接 URl 字符串应该是
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
+ "database=" + database + ";user=" + user
+ ";password=" + password + ";";
注意连接字符串中 database
和 databaseName
变量的区别。这应该有效。
更新@T.J中提到的所有要点。 Crowder的回答是正确的,你应该养成遵循标准约定的习惯。
从您的代码中可以清楚地看出,您的 ConnectionHelper 中出现了问题,它返回 null
注意:这只是我的猜测
您使用了 net.sourceforge.jtds.jdbc.Driver,那是用于 MsSql 服务器的,并且您已将 3306 作为数据库服务器的端口,大部分 3306 用于 MySql,所以您确定已在 3306
上配置您的 MsSql