我正在尝试使用 JDBC 和 Eclipse IDE 和 Java 从我的 android 连接到数据库
这是我写的代码
package Android_SQL.Package;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
import Android_SQL.Package.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;
public class Android_SQLActivity
extends Activity
implements OnClickListener
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button My_Button = (Button) this.findViewById(R.id.button1);
My_Button.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
(new Thread(new Runnable()
{
@Override
public void run()
{
// Connect
try {
String cs = "jdbc:sqlserver://192.168.0.29:1433;databaseName=ODB0336;user=sa;password=savage";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(cs);
//String sql="insert into Categories(CategoryName, Description) values(?,?)";
String sql="insert into Colors(ColorId, ColorEName, ColorName) values(?,?,?)";
PreparedStatement prepared = con.prepareStatement(sql);
prepared.setString(1, "ABCD");
prepared.setString(2, "ABCDEF");
prepared.setString(2, "XYZ");
prepared.executeUpdate();
} catch (Exception e)
{
e.printStackTrace();
Log.i("Error", e.getMessage());
}
}
}
)
).start();
}
}
但是当我连接时出现以下异常:
“com.microsoft.sqlserver.jdbc.SQLServerException:recvfrom 失败:ECONNRESET(对等连接重置)”
如何解决?问题是什么。
JDBC 版本为:Microsoft SQL Server JDBC Driver 3.0
请帮忙。