How to connect Android Studio with SQL Server database?(如何将 Android Studio 与 SQL Server 数据库连接起来?)
问题描述
我们目前遇到与此处相同的问题:如何连接Android Studio和SQL Server数据库,但是一直没有回答.
We are currently having the same problem as here: How to connect Android Studio with SQL Server database, but it hasn't been answered.
我们也在 Eclipse 上成功连接,使用 sqljdb4.jar
文件,我们在 android 上尝试了与 Eclipse 相同的代码,但它只适用于 Eclipse.
We also succeeded the connection on Eclipse, using the sqljdb4.jar
file, we tried on android the same code as in Eclipse, but it works only on Eclipse.
我们还尝试了另一个驱动程序,jtds1.3.1.jar
,但它也不起作用
We also tried another driver, jtds1.3.1.jar
, but it didn't work either
我们将登录名和密码替换为 x 和 y ;)
We replaced the login and password by x and y ;)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flux_sequence);
String url = "jdbc:jtdc:sqlserver://vps342074.ovh.net/IZY309";
//String url = "jdbc:jtdc:sqlserver://vps342074.ovh.net/MSSQLSERVER;databaseName=IZY309";
//String url = "jdbc:sqlserver://vps342074.ovh.net\MSSQLSERVER;databaseName=IZY309";
String user = "x";
String pass = "y";
try {
//TextView textViewToChange = (TextView) findViewById(R.id.textViewCol5);
//textViewToChange.setText("Hello");
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
Statement statement = con.createStatement();
ResultSet resultat = statement.executeQuery("SELECT CATEGORIE FROM dbo.PPROFIL WHERE CATEGORIE = 'ac' ");
while (resultat.next()) {
String result = resultat.getString(1);
TextView textViewToChange = (TextView) findViewById(R.id.textViewCol5);
textViewToChange.setText(result);
Log.d("My Custom Tag", result);
}resultat.close();
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static void close(Connection con) {
// TODO Auto-generated method stub
close(con);
}
这里是返回的错误:
W/System.err: java.sql.SQLException: No suitable driver
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:186)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:213)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at
com.example.gaetan.fluxsequence.flux_sequence.onCreate(flux_sequence.java:32)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.app.Activity.performCreate(Activity.java:6876)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.app.ActivityThread.access$1100(ActivityThread.java:222)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.os.Looper.loop(Looper.java:158)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7229)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
02-15 14:34:01.896 4180-4180/com.example.gaetan.fluxsequence W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
推荐答案
在连接之前,必须添加两行代码:
Before you make connection, you must add 2 lines of code:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
完整代码为:(可能)
package com.ipvsoft.sendsms.utility;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLConnection {
private static final String LOG = "DEBUG";
private static String ip = "192.168.3.85";
private static String port = "1433";
private static String classs = "net.sourceforge.jtds.jdbc.Driver";
private static String db = "THTData";
private static String un = "sa";
private static String password = "admin";
public static Connection connect() {
Connection conn = null;
String ConnURL = null;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip +":"+port+";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException e) {
Log.d(LOG, e.getMessage());
} catch (ClassNotFoundException e) {
Log.d(LOG, e.getMessage());
}
return conn;
}
}
这篇关于如何将 Android Studio 与 SQL Server 数据库连接起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Android Studio 与 SQL Server 数据库连接起来?


基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01