问题
i am new in android.. i want to connect sql server using JTDS driver. can any one tell me..
thnx in advance...
回答1:
Getting error "ClassNotFoundException" while using JTDS on ANDROID to direct access SQLSERVER?
After 3 hours RND, for finding solution for the same above error. I didnt get there is no error in the code, also I have import "jtds-1.3.0" library properly continues debugging of code still getting same error again and again.
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);
}
I tried alternative to, changing line
...... Class.forName("net.sourceforge.jtds.jdbc.Driver");
to
...... (new Driver()).getClass();
...... (new Driver())
when I tried all of these, I tought there might be a problem in jtds-1.3.0 library, and what I did, just download the old version jtds-1.2.5 and import it; and the issue resolved.
So, friends out there If you getting same error and tried different ways already, try this.
回答2:
It's weird that there is no example code on the jTDS website. I found this, it might be helpfull:
http://www.java-tips.org/other-api-tips/jdbc/how-to-connect-microsoft-sql-server-using-jdbc-3.html
import java.sql.*;
public class testConnection
{
public static void main(String[] args)
{
DB db = new DB();
db.dbConnect("jdbc:jtds:sqlserver://localhost:1433/tempdb","sa","");
}
}
class DB
{
public DB() {}
public voidn dbConnect(String db_connect_string, String db_userid, String db_password)
{
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);
System.out.println("connected");
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
EDIT:
You will get ClassNotFoundException
exception when your main class cannot be found. Find the following lines in your AndroidManifest.xml
make sure they are correct:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ezee.app"
/*...*/
<activity android:name=".connect12"
Also make sure that the class exists at your_project_folder/src/com/ezee/app/connect12
(case sensitive I think)
回答3:
in my experience, if you're using Android with a stand-alone installation of SQL Server, you must use 10.0.2.2 address instead of "localhost" or "127.0.0.1", according to Android specifics for accessing localhost servers.
I tried so, and have successfully connected to my SQL Server.
回答4:
Exception in thread "main" java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.test.objectref.GroupBy.main(GroupBy.java:12)
To solve this issue had to add Jtds lib
.
来源:https://stackoverflow.com/questions/7631758/how-to-connect-sql-server-using-jtds-driver-in-android