找不到合适的司机13

时间:2015-11-11 08:34:05

标签: android sql-server-2008 google-maps jdbc

我正在开发一个Android应用程序,它从SQL Server导入lat / long以在地图中显示为标记。我把jtds-1.21.jar放到了lib的路径上,但是我得到了错误

java.sql.SQLEXPCEPTION: No suitable driver found

这是我的代码 map.java

package com.example.ahmed.activeatm;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;

class Checker {

        /* FIELDS */

    private Activity activity;
    private Pass pass;
    private List<Resource> resourcesList;

        /* TYPES */

    public enum Resource {
        NETWORK, GPS, BLUETOOTH
    }

    public static abstract class Pass {
        public abstract void pass();
    }

        /* API */

    public Checker(Activity activity) {
        this.activity = activity;
    }

    public void check(Resource... resources) {
        resourcesList = Arrays.asList(resources);
        if (resourcesList.contains(Resource.GPS) && !isGPSActivated(activity)) {
            new AlertDialog.Builder(activity).setMessage("GPS required.").setCancelable(false).setPositiveButton("GPS", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            }).setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    activity.finish();
                }
            }).create().show();
        } else if (resourcesList.contains(Resource.NETWORK) && !isNetworkActivated(activity)) {
            new AlertDialog.Builder(activity).setMessage("Network required.").setCancelable(false).setPositiveButton("3G", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.setClassName("com.android.phone", "com.android.phone.Settings");
                    activity.startActivity(intent);
                }
            }).setNeutralButton("WiFi", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    activity.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                }
            }).setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    activity.finish();
                }
            }).create().show();

        }
    }

    public Checker pass(Pass pass) {
        this.pass = pass;
        return this;
    }

        /* PRIVATE */

    private boolean isGPSActivated(Context context) {
        return ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE)).isProviderEnabled(LocationManager.GPS_PROVIDER);
    }



    private boolean isNetworkActivated(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }

}

public class map1 extends FragmentActivity {


    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map1);
        Toast.makeText(getBaseContext(), " test ", Toast.LENGTH_LONG).show();
        setUpMapIfNeeded();
        connect();
    }






    @Override protected void onResume() {
        super.onResume();
        new Checker(this).pass(new Checker.Pass() {
            @Override public void pass() {
                //do your stuff here, do nothing outside here
            }
        }).check(Checker.Resource.GPS, Checker.Resource.NETWORK);
    }


    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    public void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_help, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){
        StringBuilder urlString = new StringBuilder();
        urlString.append("http://maps.googleapis.com/maps/api/directions/json");
        urlString.append("?origin=");// from
        urlString.append(Double.toString(sourcelat));
        urlString.append(",");
        urlString
                .append(Double.toString( sourcelog));
        urlString.append("&destination=");// to
        urlString
                .append(Double.toString( destlat));
        urlString.append(",");
        urlString.append(Double.toString( destlog));
        urlString.append("&sensor=false&mode=driving&alternatives=true");
        urlString.append("&key=YOUR_API_KEY");
        return urlString.toString();
    }




    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */


    private void setUpMap() {

        //mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.logo6))
          //      .position(new LatLng(0, 0)).title("Marker"));

        mMap.setMyLocationEnabled(true);//my location button
        mMap.getUiSettings().setZoomGesturesEnabled(true);// zoom button
       // Toast.makeText(getBaseContext(), " view map ", Toast.LENGTH_LONG).show();
        mMap.getUiSettings().setZoomControlsEnabled(true);

    }

    public void connect() {
        Connection conn = null;
        ResultSet rs = null;

       // String url = "jdbc:jtds:sqlserver://192.168.56.1:1433/lear_db;user=admim;password=admin";
        //String username = "admin";
        //String password = "admin";
        //String driver = "net.sourceforge.jtds.jdbc.Driver";
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:sqlserver://192.168.43.46:1433;databaseName=active_atm;user=admin;password=admin;");
            PreparedStatement ps;
            Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_LONG).show();

            ps=conn.prepareStatement("select x,y,status,bank_name from [atm_info]");
            ResultSet res = ps.executeQuery();
            while(res.next())
            {
                double x = Double.parseDouble(res.getString("x"));
                double y = Double.parseDouble(res.getString("y"));
                String status = res.getString("status");
                String bank = res.getString("bank_name");

                mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.logo6))
                        .position(new LatLng(x, y)).title("bank :"+bank+"  Status :"+ status));
                //res.getString("x")
            }

        } catch (Exception ex) {
            Toast.makeText(getBaseContext(), ex.toString(), Toast.LENGTH_LONG).show();
            ex.printStackTrace();
        }

    }

}

1 个答案:

答案 0 :(得分:0)

尝试使用此代码加载驱动程序:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

您应该使用SQL Server JDBC Driver 2.0

同时检查this answer

相关问题