|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在网上找了好多也没连接成功,不知道是哪出了问题
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;
}
} |
|