android连接sql2008

为了解决与梧桐数据的交互,查找很多资料,有说可以有说不可以的,众说纷纷,其实可以连接。用android接连sql2008展示可以连接的!之前用了几种方法尝试连接,最后用socket
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>

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){}
});
}
}


数据类: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();
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android连接SQL主要通过SQLite数据库来实现。SQLite是Android内置的一款轻量级关系型数据库引擎,可以在Android设备上进行本地数据库的创建、管理和操作。 在Android应用中,我们可以使用AndroidSQLiteOpenHelper类来实现与SQLite数据库连接。该类提供了一系列方法用于创建、升级和打开数据库。我们可以继承SQLiteOpenHelper类,并重写onCreate()和onUpgrade()方法来分别处理数据库的创建和升级操作。 在连接SQLite数据库之前,我们首先需要获取SQLiteOpenHelper的实例。可以通过传入数据库名、版本号和一个可选的数据库操作监听器来实例化SQLiteOpenHelper对象。在获取实例之后,我们可以通过调用getWritableDatabase()或getReadableDatabase()方法来获取可写或只读的数据库对象。 连接成功后,我们可以通过SQLiteDatabase对象执行各种数据库操作,例如创建表、插入数据、更新数据、查询数据等。可以使用execSQL()方法执行自定义的SQL语句,也可以使用insert()、update()、delete()等方法执行常见的数据库操作。通过使用Cursor对象可以获取查询结果集,并通过moveToFirst()、moveToNext()等方法遍历结果集的各个记录。 连接完成后,我们应该在适当的时候关闭数据库连接,释放资源,以免造成资源浪费。可以通过调用close()方法来关闭数据库连接。 总之,Android连接SQL主要通过SQLiteOpenHelper类和SQLiteDatabase类来实现。SQLiteOpenHelper用于创建、升级和打开数据库,而SQLiteDatabase则用于执行数据库操作。通过适时地连接和关闭数据库,我们可以方便地进行数据库管理和数据操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值