Android Studio 连接 MySQL Workbench
在网上找了好多也没连接成功,不知道是哪出了问题static/image/hrline/5.gif
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Sql{
private Connection connection;
public void getConnection() {
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:sqlserver://10.129.38.194:3306/lab?useSSL=false", "a", "qwe123rty456");
setConnection(connection);
System.out.println("数据库连接成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
/**
* 连接设置器
*/
public void setConnection(Connection set){
this.connection = set;
}
/**
* 通过数据库判断账号密码是否正确
*/
public boolean checkAccount(String ID, String password) {
getConnection();
boolean result = false;
try{
PreparedStatement stat = connection.prepareStatement("select * from laboratory_gas_security.account where ID ='" + ID + "'and password ='" + password + "'");
ResultSet rs = stat.executeQuery();
if (rs.next())
result = true;
rs.close();
connection.close();
} catch (SQLException e){
e.printStackTrace();
}
return result;
}
} 报错:
java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
页:
[1]