如何将android登录应用程序连接到mssql server 2012 [英] how to connect an android login application to mssql server 2012
本文介绍了如何将android登录应用程序连接到mssql server 2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将我的登录android应用程序连接到MSSQL server 2012 r2,但无法连接它。我请求大家帮助我解决这个问题并提供最佳解决方案,以便将应用程序与MSSQL服务器连接起来。
我也试过parallelcodes.com但它没有用。
请尽快帮助。
谢谢,
Servesh Tiwari
I have tried to connect my login android application to MSSQL server 2012 r2 but fail to connect with it. I request you all in helping me out of this and give the best solutions to connect the app with MSSQL server.
I have also tried parallelcodes.com but it was not working.
Pls help as soon as possible.
Thanks,
Servesh Tiwari
推荐答案
将Android连接到MS SQL数据库。
包app.mysqlapp;
包app.mysqlapp;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support .v7.app.ActionBarActivity;
import android.util.Log;
import android.view .View;
导入android.widget.Button;
导入android.widget.EditText; < br $> b $ b
导入android.widget.TextView;
import java.sql.Connection;
导入java.sql。 DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
公共类登录扩展ActionBarActivity {
按钮loginbtn;
TextView errorlbl;
EditText edname,edpassword;
连接连接;
PreparedStatement preparedStatement;
语句st;
字符串ipaddress ,db,用户名,密码;
@SuppressLint(NewApi)
private Connection ConnectionHelper( String user,String password,
String database,String server){b / b
StrictMode.ThreadPolicy policy =新的StrictMode.ThreadPolicy.Buil der()
.permitAll()。build();
StrictMode.setThreadPolicy(policy) ;
连接连接= null;
String ConnectionURL = null;
尝试{
Class.forName(net.sourceforge.jtds.jdbc.Driver);
+databaseName =+ database +; user =+ user
+; password =+ password + ;;
connection = DriverManager.getConnection(ConnectionURL);
} catch(SQLException se ){
Log.e(ERRO,se.getMessage());
} catch(ClassNotFoundException e){
Log.e(ERRO,e.getMessage());
} catch(例外e){
Log.e(ERRO, e.getMessage());
}
返回连接;
}
@Override
protected void onCreate(Bundle savedInstanceState )$
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginbtn =(按钮)findViewById(R.id.btnlogin);
errorlbl =(TextView )findViewById(R.id.lblerror);
edname =(EditText)findViewById(R.id.txtname);
edpassword =(EditText)findViewById(R.id.txtpassword);
ipaddress =192.168.0.14;
db =mydatabase;
username =hitesh;
密码=789;
connect = ConnectionHelper(用户名,密码,db,ipad)礼服);
loginbtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(查看v){
尝试{
connect = ConnectionHelper(用户名,密码,db,ipaddress);
st = connect.createStatement();
ResultSet rs = st.executeQuery(select * from login where userid ='+ edname.getText()。toString()+'and password ='+ edpassword.getText() .toString()+');
if(rs!= null&& rs.next()){
意图i =新意图(Login.this,MainActivity.class);
startActivity(i);
}否则{
errorlbl.setText(抱歉,错误的凭证!!!);
}
} catch(SQLException e){< br $>
errorlbl.setText(e.getMessage()。toString());
}
}
});
}
}
Connect Android to MS SQL Database.
package app.mysqlapp;
package app.mysqlapp;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Login extends ActionBarActivity {
Button loginbtn;
TextView errorlbl;
EditText edname, edpassword;
Connection connect;
PreparedStatement preparedStatement;
Statement st;
String ipaddress, db, username, password;
@SuppressLint("NewApi")
private Connection ConnectionHelper(String user, String password,
String database, String server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
+ "databaseName=" + database + ";user=" + user
+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
} 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 connection;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginbtn = (Button) findViewById(R.id.btnlogin);
errorlbl = (TextView) findViewById(R.id.lblerror);
edname = (EditText) findViewById(R.id.txtname);
edpassword = (EditText) findViewById(R.id.txtpassword);
ipaddress = "192.168.0.14";
db = "mydatabase";
username = "hitesh";
password = "789";
connect = ConnectionHelper(username, password, db, ipaddress);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
connect = ConnectionHelper(username, password, db, ipaddress);
st = connect.createStatement();
ResultSet rs = st.executeQuery("select * from login where userid='" + edname.getText().toString() + "' and password='" + edpassword.getText().toString() + "'");
if (rs != null && rs.next()) {
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
} else {
errorlbl.setText("Sorry, wrong credentials!!!");
}
} catch (SQLException e) {
errorlbl.setText(e.getMessage().toString());
}
}
});
}
}
以上代码工作正常请使用此代码,如果仍有问题,请随时与我联系。我的电子邮件地址是serveh20@gmail.com。
Above code is working fine Please use this code and if any problem persists feel free to contact me. my email address is servesh20@gmail.com.
这篇关于如何将android登录应用程序连接到mssql server 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文