android连接sql2008

 为了解决与梧桐数据的交互,查找很多资料,有说可以有说不可以的,众说纷纷,其实可以连接。用android接连sql2008展示可以连接的!之前用了几种方法尝试连接,最后用socket
main.cml
Java代码 android连接sql2008 android连接sql2008android连接sql2008
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:orientation="vertical"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent"  
  6. >   
  7. <TextView    
  8. android:id="@+id/TextView01"    
  9. android:layout_width="fill_parent"    
  10. android:layout_height="wrap_content"    
  11. android:text="这里显示接收到服务器发来的信息"  
  12. />   
  13. <EditText    
  14. android:id="@+id/name"    
  15. android:text="名字"    
  16. android:layout_width="fill_parent"    
  17. android:layout_height="wrap_content">   
  18. </EditText>   
  19.   
  20. <EditText   
  21. android:id="@+id/address"  
  22. android:text="输入address"  
  23. android:layout_width="fill_parent"  
  24. android:layout_height="wrap_content" >   
  25.   
  26. <requestFocus />   
  27. </EditText>   
  28.   
  29. <EditText   
  30. android:id="@+id/phone"  
  31. android:text="123456"  
  32. android:layout_width="fill_parent"  
  33. android:layout_height="wrap_content"  
  34. android:inputType="phone" />   
  35.   
  36. <LinearLayout   
  37. android:id="@+id/linearLayout1"  
  38. android:layout_width="fill_parent"  
  39. android:layout_height="wrap_content" >   
  40.   
  41. <Button   
  42. android:id="@+id/save"  
  43. android:layout_width="154dp"  
  44. android:layout_height="wrap_content"  
  45. android:text="保存" />   
  46.   
  47. <Button   
  48. android:id="@+id/select"  
  49. android:layout_width="154dp"  
  50. android:layout_height="wrap_content"  
  51. android:text="查询" />   
  52.   
  53. </LinearLayout>   
  54.   
  55. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView 
android:id="@+id/TextView01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="这里显示接收到服务器发来的信息"
/>
<EditText 
android:id="@+id/name" 
android:text="名字" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">
</EditText>

<EditText
android:id="@+id/address"
android:text="输入address"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/phone"
android:text="123456"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone" />

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/save"
android:layout_width="154dp"
android:layout_height="wrap_content"
android:text="保存" />

<Button
android:id="@+id/select"
android:layout_width="154dp"
android:layout_height="wrap_content"
android:text="查询" />

</LinearLayout>

</LinearLayout>

Activity01.java
package com.android.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

import com.yarin.android.Examples_08_04.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity01 extends Activity
{
private final String DEBUG_TAG = "Activity01";

private TextView mTextView=null;
private EditText nameEdit=null;
private Button save=null;
private Button select=null;
private EditText addressEdit=null;
private EditText phoneEdit=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

save = (Button)findViewById(R.id.save);
select=(Button)findViewById(R.id.select);
mTextView=(TextView)findViewById(R.id.TextView01);
nameEdit=(EditText)findViewById(R.id.name);
addressEdit=(EditText)findViewById(R.id.address);
phoneEdit=(EditText)findViewById(R.id.phone);



//登陆
save.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Socket socket = null;
String name=nameEdit.getText().toString();
String address=addressEdit.getText().toString();
String phone=phoneEdit.getText().toString();
String message = name + "\n"+address+"\n"+phone+"\n";

try
{
//创建Socket
socket = new Socket("192.168.0.7",54321);
//向服务器发送消息
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);

//接收来自服务器的消息
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = br.readLine();

if ( msg != null )
{
mTextView.setText(msg);
}
else
{
mTextView.setText("数据错误!");
}
//关闭流
out.close();
br.close();
//关闭Socket
socket.close();
}
catch (Exception e)
{
// TODO: handle exception
Log.e(DEBUG_TAG, e.toString());
}
}
});
select.setOnClickListener(new OnClickListener()
{
public void onClick(View v){}
});
}
}
服务端:
主类:Server.java
Java代码 android连接sql2008 android连接sql2008android连接sql2008
  1. package com.android.client;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.BufferedWriter;   
  5. import java.io.InputStreamReader;   
  6. import java.io.OutputStreamWriter;   
  7. import java.io.PrintWriter;   
  8. import java.io.UnsupportedEncodingException;   
  9. import java.net.Socket;   
  10. import com.yarin.android.Examples_08_04.R;   
  11. import android.app.Activity;   
  12. import android.os.Bundle;   
  13. import android.util.Log;   
  14. import android.view.View;   
  15. import android.view.View.OnClickListener;   
  16. import android.widget.Button;   
  17. import android.widget.EditText;   
  18. import android.widget.TextView;   
  19. public class Activity01 extends Activity   
  20. {   
  21. private final String  DEBUG_TAG = "Activity01";   
  22.   
  23. private TextView mTextView=null;   
  24. private EditText nameEdit=null;   
  25. private Button  save=null;   
  26. private Button select=null;   
  27. private EditText addressEdit=null;   
  28. private EditText phoneEdit=null;   
  29. /** Called when the activity is first created. */  
  30. @Override  
  31. public void onCreate(Bundle savedInstanceState)   
  32. {   
  33.   super.onCreate(savedInstanceState);   
  34.   setContentView(R.layout.main);   
  35.      
  36.   save = (Button)findViewById(R.id.save);   
  37.   select=(Button)findViewById(R.id.select);   
  38.   mTextView=(TextView)findViewById(R.id.TextView01);   
  39.   nameEdit=(EditText)findViewById(R.id.name);   
  40.   addressEdit=(EditText)findViewById(R.id.address);   
  41.   phoneEdit=(EditText)findViewById(R.id.phone);   
  42.      
  43.      
  44.      
  45.   //登陆   
  46.   save.setOnClickListener(new OnClickListener()   
  47.   {   
  48.    public void onClick(View v)   
  49.    {   
  50.     Socket socket = null;   
  51.     String name=nameEdit.getText().toString();   
  52.     String address=addressEdit.getText().toString();   
  53.     String phone=phoneEdit.getText().toString();   
  54.     String message = name + "\n"+address+"\n"+phone+"\n";       
  55.        
  56.     try    
  57.     {    
  58.      //创建Socket   
  59.      socket = new Socket("192.168.0.7",54321);    
  60.      //向服务器发送消息   
  61.      PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);         
  62.      out.println(message);    
  63.         
  64.      //接收来自服务器的消息   
  65.      BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));    
  66.      String msg = br.readLine();    
  67.         
  68.      if ( msg != null )   
  69.      {   
  70.       mTextView.setText(msg);   
  71.      }   
  72.      else  
  73.      {   
  74.       mTextView.setText("数据错误!");   
  75.      }   
  76.      //关闭流   
  77.      out.close();   
  78.      br.close();   
  79.      //关闭Socket   
  80.      socket.close();    
  81.     }   
  82.     catch (Exception e)    
  83.     {   
  84.      // TODO: handle exception   
  85.      Log.e(DEBUG_TAG, e.toString());   
  86.     }   
  87.    }   
  88.   });   
  89.   select.setOnClickListener(new OnClickListener()   
  90.   {   
  91.    public void onClick(View v){}   
  92. });   
  93. }    
  94. }  
package com.android.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import com.yarin.android.Examples_08_04.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity01 extends Activity
{
private final String  DEBUG_TAG = "Activity01";

private TextView mTextView=null;
private EditText nameEdit=null;
private Button  save=null;
private Button select=null;
private EditText addressEdit=null;
private EditText phoneEdit=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  save = (Button)findViewById(R.id.save);
  select=(Button)findViewById(R.id.select);
  mTextView=(TextView)findViewById(R.id.TextView01);
  nameEdit=(EditText)findViewById(R.id.name);
  addressEdit=(EditText)findViewById(R.id.address);
  phoneEdit=(EditText)findViewById(R.id.phone);
  
  
  
  //登陆
  save.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v)
   {
    Socket socket = null;
    String name=nameEdit.getText().toString();
    String address=addressEdit.getText().toString();
    String phone=phoneEdit.getText().toString();
    String message = name + "\n"+address+"\n"+phone+"\n";    
    
    try 
    { 
     //创建Socket
     socket = new Socket("192.168.0.7",54321); 
     //向服务器发送消息
     PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);      
     out.println(message); 
     
     //接收来自服务器的消息
     BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     String msg = br.readLine(); 
     
     if ( msg != null )
     {
      mTextView.setText(msg);
     }
     else
     {
      mTextView.setText("数据错误!");
     }
     //关闭流
     out.close();
     br.close();
     //关闭Socket
     socket.close(); 
    }
    catch (Exception e) 
    {
     // TODO: handle exception
     Log.e(DEBUG_TAG, e.toString());
    }
   }
  });
  select.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v){}
});
} 
}


数据类:ConSQL.java
Java代码 android连接sql2008 android连接sql2008android连接sql2008
  1. 驱动包:sqljdbc4.jar支持1.6以上   
  2.   
  3. import java.sql.*;   
  4.   
  5. public class ConSQL   
  6. {   
  7.         Statement stmt;   
  8.          Connection con;   
  9.   
  10.         public void ConSQl(){   
  11.                   String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎   
  12.                   String connectDB="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Mydb";//数据源   
  13.                   try  
  14.                   {   
  15.                    Class.forName(JDriver);//加载数据库引擎,返回给定字符串名的类   
  16.                   }catch(ClassNotFoundException e)   
  17.                   {   
  18.                    e.printStackTrace();   
  19.                      
  20.   
  21.                    System.out.println("加载数据库引擎失败");   
  22.                    System.exit(0);   
  23.                   }        
  24.                   System.out.println("加载驱动成功");   
  25.                    
  26.                      
  27.                   try  
  28.                   {   
  29.                    String user="sa";   
  30.                    String password="hello2010";   
  31.                    con=DriverManager.getConnection(connectDB,user,password);//连接数据库对象   
  32.                    System.out.println("连接数据库成功");   
  33.                       
  34.                       
  35.                   }catch(SQLException e){   
  36.                           e.printStackTrace();   
  37.                              
  38.                            System.exit(0);   
  39.                   }   
  40.         }   
  41.         /****  
  42.          * 查询*/  
  43.         public void selete(){   
  44.                         try {   
  45.                                 stmt=(Statement) con.createStatement();//创建SQL命令对象     
  46.                     String sql="select * from order";//查询order表语句   
  47.                     ResultSet rs=stmt.executeQuery(sql);//执行查询   
  48.                     StringBuilder str=new StringBuilder();   
  49.                     while(rs.next()){   
  50.                      str.append(rs.getString(1)+"\n");    
  51.                     }   
  52. //                    mSetText(str.toString());   
  53.                    
  54.                     rs.close();       
  55.                     stmt.close();   
  56.                     con.close();   
  57.                        
  58.                  } catch (Exception e) {   
  59.                          e.printStackTrace();   
  60.                  }   
  61.         }   
  62.         /**  
  63.          * 注入信息  
  64.          * fhwd:发货网点,shwd:收货网点,ydh:运单号,kh:卡号,  
  65.          * dshk:代收货款,fhname:发货人,fhphone:发货电话,shname:收货人,shphone:收货电话  
  66.          * yf:运费;yhf:运货费,hwname:货物名称,bz:包装件,bs:保值,hdan:回单标记*/  
  67.         public void insert(String fhwd,String shwd,String ydh,String kh,String dshk,String fhname,String fhphone,String shname,Number shphone   
  68.                         ,String yf,String yhf,String hwname,String bz,String num,String bs,int hdan){   
  69.                    
  70.                 String sql="INSERT INTO order VALUES(fhwd,shwd,ydh,kh,dshk,fhname,fhphone,shname,shphone,yf,yhf,hwname,bz,num,bs,hdan)";   
  71.                 try {   
  72.                    
  73.                         stmt.executeUpdate(sql);   
  74.                 } catch (SQLException e) {   
  75.                         System.out.println("数据库注入失败!");   
  76.                         e.printStackTrace();   
  77.                 }   
  78.         }   
  79.         public void insert(String name) {   
  80.                 // TODO Auto-generated method stub                   
  81.                 try {   
  82.                         System.out.println("name:"+name);   
  83.                         stmt.executeUpdate(name);   
  84.                 } catch (SQLException e) {   
  85.                         System.out.println("数据库注入失败!");   
  86.                         e.printStackTrace();   
  87.                 }   
  88.                    
  89.         }   
  90. }   
上一篇:Java实现读取文件夹下(包括子目录)所有文件的文件名


下一篇:SQL连接查询深度探险