main.cml
- <?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>
<?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
- 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){}
- });
- }
- }
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
- 驱动包:sqljdbc4.jar支持1.6以上
- import java.sql.*;
- public class ConSQL
- {
- Statement stmt;
- Connection con;
- public void ConSQl(){
- String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
- String connectDB="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Mydb";//数据源
- try
- {
- Class.forName(JDriver);//加载数据库引擎,返回给定字符串名的类
- }catch(ClassNotFoundException e)
- {
- e.printStackTrace();
- System.out.println("加载数据库引擎失败");
- System.exit(0);
- }
- System.out.println("加载驱动成功");
- try
- {
- String user="sa";
- String password="hello2010";
- con=DriverManager.getConnection(connectDB,user,password);//连接数据库对象
- System.out.println("连接数据库成功");
- }catch(SQLException e){
- e.printStackTrace();
- System.exit(0);
- }
- }
- /****
- * 查询*/
- public void selete(){
- try {
- stmt=(Statement) con.createStatement();//创建SQL命令对象
- String sql="select * from order";//查询order表语句
- ResultSet rs=stmt.executeQuery(sql);//执行查询
- StringBuilder str=new StringBuilder();
- while(rs.next()){
- str.append(rs.getString(1)+"\n");
- }
- // mSetText(str.toString());
- rs.close();
- stmt.close();
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 注入信息
- * fhwd:发货网点,shwd:收货网点,ydh:运单号,kh:卡号,
- * dshk:代收货款,fhname:发货人,fhphone:发货电话,shname:收货人,shphone:收货电话
- * yf:运费;yhf:运货费,hwname:货物名称,bz:包装件,bs:保值,hdan:回单标记*/
- public void insert(String fhwd,String shwd,String ydh,String kh,String dshk,String fhname,String fhphone,String shname,Number shphone
- ,String yf,String yhf,String hwname,String bz,String num,String bs,int hdan){
- String sql="INSERT INTO order VALUES(fhwd,shwd,ydh,kh,dshk,fhname,fhphone,shname,shphone,yf,yhf,hwname,bz,num,bs,hdan)";
- try {
- stmt.executeUpdate(sql);
- } catch (SQLException e) {
- System.out.println("数据库注入失败!");
- e.printStackTrace();
- }
- }
- public void insert(String name) {
- // TODO Auto-generated method stub
- try {
- System.out.println("name:"+name);
- stmt.executeUpdate(name);
- } catch (SQLException e) {
- System.out.println("数据库注入失败!");
- e.printStackTrace();
- }
- }
- }