1

I'm trying to connect to a database on another server which is reacheable, in fact I can connect and view the data using the Microsoft Access through an ODBC connection. However I can not do it using a very simple java code, which you can see below.

Anyway, I downloaded the SQLServer JDBC Microsoft 3.0 and I had installed in C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\esn. After that I've also added to the project classpath from Eclipse with the Project open --> Project --> Properties --> Java Buid Path --> Libraries --> Add external Jars --> select the jar file "sqljdbc4" of the same path (C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\esn).

However when I put in Dbugger in the line Class.forName ... Skip to} catch .. seems has a problem, and the connection "con" always is null.

12-14 19:47:38.054: E/MyActivity(13682): java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver 12-14 19:47:38.054: E/MyActivity(13682): at java.lang.Class.classForName(Native Method) 12-14 19:47:38.054: E/MyActivity(13682): at java.lang.Class.forName(Class.java:217) 12-14 19:47:38.054: E/MyActivity(13682): at java.lang.Class.forName(Class.java:172) 12-14 19:47:38.054: E/MyActivity(13682): at com.example.conectar.ConexionSQL.main(ConexionSQL.java:26) 12-14 19:47:38.054: E/MyActivity(13682): at com.example.conectar.MainActivity.onCreate(MainActivity.java:23) 12-14 19:47:38.054: E/MyActivity(13682): at android.app.Activity.performCreate(Activity.java:5008) 12-14 19:47:38.054: E/MyActivity(13682): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 12-14 19:47:38.054: E/MyActivity(13682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 12-14 19:47:38.054: E/MyActivity(13682): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 12-14 19:47:38.054: E/MyActivity(13682): at android.app.ActivityThread.access$600(ActivityThread.java:130)

Moreover I added enviorement variables in Windows 7 32 bits in user and system. CLASSPATH=.;C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\esn\sqljdbc4.jar

Please, could you help me?

Thanks in advance.

package com.jose.sqlserver;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.sql.*;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Connection con = null;

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String connectionUrl = "jdbc:sqlserver://192.168.1.24:1433;" +
                   "databaseName=MisDatos;user=sa;password=1234;";
        con = DriverManager.getConnection(connectionUrl);
        PreparedStatement ps1=con.prepareStatement("select top 5 * from Usuarios");
        ResultSet rs1=ps1.executeQuery();
        while (rs1.next()) {

        }


    }
    catch (Exception e){}
}
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.