674
技術社區[雲棲]
android連接sql2008
為了解決與梧桐數據的交互,查找很多資料,有說可以有說不可以的,眾說紛紛,其實可以連接。用android接連sql2008展示可以連接的!之前用了幾種方法嚐試連接,最後用socketmain.cml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://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:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="這裏顯示接收到服務器發來的信息" /> <EditText android: android:text="名字" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <EditText android: android:text="輸入address" android:layout_width="fill_parent" android:layout_height="wrap_content" > <requestFocus /> </EditText> <EditText android: android:text="123456" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" /> <LinearLayout android: android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android: android:layout_width="154dp" android:layout_height="wrap_content" android:text="保存" /> <Button android: 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();
- }
- }
- }
最後更新:2017-04-02 06:52:09
上一篇:
華為JAVA麵試題及詳解
下一篇:
我的appstore新遊戲--LeBallon 拿碼了
《數據分析實戰 基於EXCEL和SPSS係列工具的實踐》一3.3 耗時耗力的數據整理過程
常見麵試之機器學習算法思想簡單梳理
layout、layout-land和layout-port的區別
《數據結構與抽象:Java語言描述(原書第4版)》一P.3 Java接口
設計模式六大原則---單一職責原則
Yann LeCun說是時候放棄概率論了,因果關係才是理解世界的基石
MySql分區表性能測試及切換案例
Large-Scale Instant Messaging Hosting on Alibaba Cloud
華爾街(wall street) 之 (一) 資本無眠
如何選擇合適的數據庫,讓遊戲更高效可用