SQL服务器连接 - 如何?

时间:2018-03-07 16:53:48

标签: android sql-server

我尝试将SQL服务器连接到我的Android应用程序,但它显示错误。我不能写输出。我创建了一个数据库'尝试'在MS SQL Server和表名称中是“登录”。我正在使用安装在我的PC上的Microsoft SQL Server。帮帮我

我的代码:

public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener {
 EditText username,password;
 Connection con;
@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      username=(EditText)findViewById(R.id.username);
      password=(EditText)findViewById(R.id.password);
      findViewById(R.id.submit).setOnClickListener(this);
   }

   @Override
  public void onClick(View v) {

    String query = "select * from login where userid= '" + 
    username.getText().toString() + "' and password = '"+ 
  password.getText().toString() +"'  ";
      try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String 
     url="jdbc:jtds:sqlserver://
  192.168.0.102;databaseName=try;username=sa;password=aakash@123";
        con = DriverManager.getConnection(url); // line 57
        Log.d("Connection : ","Connected");
     Statement   stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        if(rs.next())
        {
            Toast.makeText(this, "Login", Toast.LENGTH_SHORT).show();

            con.close();
        }
        else
        {
            Toast.makeText(this, "Login fail", Toast.LENGTH_SHORT).show();
        }
    }  catch (java.sql.SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

} 

我不知道这是什么错误? 错误:

   E/art: Failed sending reply to debugger: Broken pipe
   AndroidRuntime: FATAL EXCEPTION: main

    android.os.NetworkOnMainThreadException

    at android.os.StrictMode$
     AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)

   at java.net.AbstractPlainSocketImpl.
   doConnect(AbstractPlainSocketImpl.java:333)

   at java.net.AbstractPlainSocketImpl.
  connectToAddress(AbstractPlainSocketImpl.java:196)

  at java.net.AbstractPlainSocketImpl.
   connect(AbstractPlainSocketImpl.java:178)                                                                              
   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
        at java.net.Socket.connect(Socket.java:605)                                                                            
    at net.sourceforge.jtds.jdbc.SharedSocket.
    createSocketForJDBC3(SharedSocket.java:288)                                                                                  
  at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)                                                                               
  at net.sourceforge.jtds.jdbc.JtdsConnection.<init>
  (JtdsConnection.java:331)                                                                               
  at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)                                                                               
  at java.sql.DriverManager.getConnection(DriverManager.java:569)           
  at java.sql.DriverManager.getConnection(DriverManager.java:237)                                                                              
  at com.software.aj.clgproject.MainActivity.onClick(MainActivity.java:57)                                                                             

请帮我解决这个错误。谢谢。

0 个答案:

没有答案