我在这里使用 Eclipse 是我的 MainActivity.java 代码:
package com.example.tutorialsqlserver_android;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity
{
EditText txtErrMsg;
EditText txtData;
Connection connect;
private Connection CONN( )
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
String errmsg = null;
txtErrMsg.setText("action");
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
ConnURL = "jdbc:jtds:sqlserver://10.114.1.129/master;user=sa;password=***I now my password thanks***;";
conn = DriverManager.getConnection(ConnURL);
}
catch (SQLException se)
{
errmsg = se.getMessage() + " " + se.getErrorCode() + " bitti";
txtErrMsg.setText(errmsg);
Log.e("ERRO",errmsg);
return null;
}
catch (ClassNotFoundException e)
{
errmsg = "B1 " + e.getMessage() + " " + e.getLocalizedMessage() + " bitti1";
txtErrMsg.setText(errmsg);
Log.e("ERRO",errmsg);
return null;
}
catch (Exception e)
{
errmsg = "B2 " + e.toString() + " bitti2";
txtErrMsg.setText(errmsg);
Log.e("ERRO",errmsg);
return null;
}
finally
{
txtErrMsg.setText("yemedi ");
}
txtErrMsg.setText("OK ");
return conn;
}
public void QuerySQL(String COMANDOSQL)
{
ResultSet rs;
try
{
Statement statement = connect.createStatement();
rs = statement.executeQuery(COMANDOSQL);
while(rs.next())
{
txtData.setText(rs.getString("Name"));
}
}
catch (Exception e)
{
Log.e("ERRO",e.getMessage());
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtErrMsg = (EditText) findViewById(R.id.txtErrMsg);
txtData = (EditText) findViewById(R.id.txtData);
connect = CONN();
if(connect != null)
{
txtData.setText("OlacakInsallah");
//QuerySQL("select * from Students");
}}}
我将 jtds-1.2.7.jar 文件放在我的项目 Android Private Libraries>>jtds-1.2.7.jar 下
我的 activity_main.xml 代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txt_conteudo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Metin" />
<EditText
android:id="@+id/txtErrMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/txt_conteudo"
android:inputType="text"
android:text="Ilk" />
<EditText
android:id="@+id/txtData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtErrMsg"
android:layout_alignParentRight="true"
android:layout_below="@+id/txtErrMsg"
android:layout_marginTop="143dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
我的sql版本:youUpload.org/fazsgrh 我的sql登录:youUpload.org/e32xkba
我的错在哪里?谢谢你。此应用程序现在无法运行。