如何通过android在sql server中插入动态值

时间:2014-11-19 06:35:50

标签: android sql-server-2008-r2

public void query2(View V) 
  { 
      Log.i("Android"," MySQL Connect Example."); 
      Connection conn = null; 
      try { 
          String driver = "net.sourceforge.jtds.jdbc.Driver"; 
          Class.forName(driver).newInstance(); 
          String connString = "jdbc:jtds:sqlserver://10.0.2.2:1433/demo;integratedSecurity=true;user=usd;password=dell@123;";
           conn = DriverManager.getConnection(connString); 
          Log.w("Connection","open"); 

    //  Statement stmt = conn.createStatement(); 
    PreparedStatement ps;
     ps=conn.prepareStatement("insert into UserMaster1 values(?,?)");
     //int x=stmt.executeUpdate("insert into UserMaster1 values( 'usaid','mansoori')");
     ps.setString(1,un);
        ps.setString(2,pwd);
     Log.d("exupdt","insertion done");
     Toast.makeText(this, "Success", Toast.LENGTH_LONG).show();



    ps.executeUpdate();

                        Log.d("PreparedStmt","Success");
      conn.close();  
      } 
  catch (Exception e)
    { 
      Log.w("Error connection","" + e.getMessage()); }
  }

  }

我在sql server中插入数据是通过android使用接口PreparedStatement到sql server但是只插入一个空白行而不是数据如何插入动态值

2 个答案:

答案 0 :(得分:0)

您无法直接访问远程数据库。

您必须在服务器端编写Web服务,然后将Android应用程序的请求发送到此服务。

您可以使用此tutorial来实现此类程序。

答案 1 :(得分:0)

public class MainActivity extends Activity {

   private TextView t1,t2,t3; 
   private Button b1;
   private EditText e1,e2;
   String un,pwd;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1=(TextView)findViewById(R.id.tv1);
    t2=(TextView)findViewById(R.id.tv2);
    t3=(TextView)findViewById(R.id.tv3);
    b1=(Button)findViewById(R.id.btn);
    e1=(EditText)findViewById(R.id.ed1);
    e2=(EditText)findViewById(R.id.ed2);

     if (android.os.Build.VERSION.SDK_INT> 9){
          StrictMode.ThreadPolicy policy =new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
        }
}

  public void query2(View V) 
  { 
      un =e1.getText().toString();
      pwd =e2.getText().toString();
     if(un.equals("")||pwd.equals(""))
         Toast.makeText(this,"Above Fields cannot be left blank",Toast.LENGTH_LONG).show();
     else
     {
       Connection conn = null; 
      try { 
          String driver = "net.sourceforge.jtds.jdbc.Driver"; 
          Class.forName(driver).newInstance(); 
          String connString = "jdbc:jtds:sqlserver://172.22.0.2:1433/Samay;integratedSecurity=true;user=DBSQL;password=samay@123;";
           conn = DriverManager.getConnection(connString); 
          Log.w("Connection","open"); 
       PreparedStatement ps;
       ps=conn.prepareStatement("insert into UserMaster1 values(?,?)");
       ps.setString(1,un);
       ps.setString(2,pwd);
       int x;
       x=ps.executeUpdate();
      Log.d("exupdt",un);
      Log.d("exupdt",pwd);
 Toast.makeText(this, "Success"+x, Toast.LENGTH_LONG).show();
 Log.d("PreparedStmt","Success");
      conn.close();  
      } 
  catch (Exception e)
    { 
      Log.w("Error connection","" + e.getMessage()); }
  }

  }

}

我最好的朋友非常逻辑地解决这个典型的问题我不明白你为什么不给出确切的答案而不是继承

我最好的朋友名字是s。(java女王)她只是通过给予

来解决这个问题
 un =e1.getText().toString();
      pwd =e2.getText().toString();

这个代码在方法query2和程序之后给出了我想要的确切结果..我有动态插入android到sql server ...