I am try to connect to sqlserver in android studio if i use variavle like this:
String server ="192.168.1.2";
String database = "Db2020";
String user = "sa";
String password = "23457633";
it worked fine and conncted, put when read variavle from file like this:
ReadFile readFile=new ReadFile(fileEvents);
server=readFile.servername;
database=readFile.database;
user=readFile.username;
password=readFile.password;
it not conncted. i do not way?????
hear my code :
public class 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++;
}
}
}
}
class Connectionclass:
public class Connectionclass {
@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;
}
}
public class SettingActivity extends 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();
}
}
}