【发布时间】:2022-01-21 12:14:36
【问题描述】:
如果我像这样使用 variavle,我会尝试连接到 android studio 中的 sqlserver:
String server ="192.168.1.2";
String database = "Db2020";
String user = "sa";
String password = "23457633";
它工作正常并且连接,当从这样的文件中读取变量时放置:
ReadFile readFile=new ReadFile(fileEvents);
server=readFile.servername;
database=readFile.database;
user=readFile.username;
password=readFile.password;
它没有连接。 我没有办法??????
听听我的代码:
公共类 ReadFile {
String servername ,username,password,database;
public ReadFile (String fileEvents) {
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(fileEvents));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
} catch (
IOException e) {
}
String result = text.toString();
int xStart = 0, xOptin = 1;
for (int xx = 0; xx != result.length(); xx = xx + 1) {
if (result.substring(xx, xx + 1).equals("-")) {
if (xOptin == 1) {
servername = result.substring(xStart, xx -1);
}
if (xOptin == 2) {
username = result.substring(xStart, xx-1);
}
if (xOptin == 3) {
password = result.substring(xStart, xx-1);
}
if (xOptin == 4) {
database = result.substring(xStart, xx-1);
}
xStart = xx + 2;
xOptin++;
}
}
}
}
类连接类:
公共类连接类 {
@SuppressLint("NewApi")
String server ="192.168.1.2";
String database = "Db2020";
String user = "sa";
String password = "23457633";
public Connection Conn(String fileEvents){
ReadFile readFile=new ReadFile(fileEvents);
server=readFile.servername;
database=readFile.database;
user=readFile.username;
password=readFile.password;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn=null;
String ConnectionURL =null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
ConnectionURL = "jdbc:jtds:sqlserver://"+server+";databaseName="+database+";user="+user+";password="+password;
conn= DriverManager.getConnection(ConnectionURL);
}catch (SQLException se){
}catch (ClassNotFoundException e){
}catch (Exception e){}
return conn;
}
}
公共类 SettingActivity 扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
File fileEvents = new File( getFilesDir()+"/text/sample");
Connectionclass con = new Connectionclass();
conn = con.Conn(fileEvents.toString());
if (conn == null) {
Toast.makeText(getApplicationContext(), "Connection Not Establish...", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), " Connection Establish...", Toast.LENGTH_LONG).show();
}
}
}
【问题讨论】:
标签: java android sql sql-server android-studio