Код: Выделить всё
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* Created by H-PC on 16-Oct-15.
*/
public class ConnectionClass {
String ip = "******";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "IDB";
String un = "sa";
String password = "admin123";
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} 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 conn;
}
}
-------------------------------------------------------------------------------
< /code>
[b] mainactivity.java:[/b]
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.sql.Connection;
public class MainActivity extends AppCompatActivity {
Button button1;
TextView txtView1;
EditText editText1;
ConnectionClass connectionClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionClass = new ConnectionClass();
button1=(Button)findViewById(R.id.btnSearch);
txtView1=(TextView)findViewById(R.id.searchLbl);
editText1=(EditText)findViewById(R.id.txtSearch);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String newValue = editText1.getText().toString().trim();
if (newValue.toString().trim().equals("")) { < b r / > t x t V i e w 1 . s e t T e x t ( " P l e a s e E n t e r I D " ) ; < b r / > } < b r / > e l s e < b r / > { < b r / > C o n n e c t i o n c o n = c o n n e c t i o n C l a s s . C O N N ( ) ; < b r / > < b r / > i f ( c o n = = n u l l ) < b r / > { < b r / > S t r i n g m s g = " E r r o r i n S Q L C o n n e c t i o n " ; < b r / > } < b r / > e l se
{
String query= "Select * From Employees Where EmpId='"+newValue+"'";
}
}
}
});
}
}
---------------------------------------------------------------------------
< /code>
[b] text < /strong> < /p>
-------------------------------------------------------------------------------
< /code>
таблица (сотрудники): < /strong> < /p>
EmpId EmpName Designation Gender Age Mob Address
----- ------- ----------- ------ --- --- -------
1 asd ttt M 30 fghfhfh
2 dfg yyy F 26 fhgfhfhf
3 dfhh ppp M 47 fghhfghf
--------------------------------------------------------------------------------
Подробнее здесь: https://stackoverflow.com/questions/345 ... in-android
Мобильная версия