当前位置: 首页 > 知识库问答 >
问题:

找不到合适的驱动程序13

樊杰
2023-03-14

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

java.sql.SQLEXPCEPTION: No suitable driver found

这是我的代码图。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个答案

宗政法
2023-03-14

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

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

您应该使用SQL Server JDBC驱动程序2.0。

请检查此答案。

 类似资料:
  • 问题内容: 我希望有一个人可以帮助我。我正在研究一个与SQLite数据库连接的简单应用程序。以下是我的连接代码: 这只是一个在数据库中加载用户名和密码的窗口。我的问题是,当我单击按钮时,出现以下异常: (我找到了一个有关如何使用Java连接到SQLite数据库的示例,我发现该示例运行良好) 这个程序我正在窗口构建器(日食)中做。我使用的示例中使用的驱动程序相同。我不知道我是否必须使用其他驱动程序。

  • 问题内容: 这个问题已经在这里有了答案 : 臭名昭著的java.sql.SQLException:未找到合适的驱动程序 (13个答案) 4年前关闭。 我有一个Java类,该类通过在Tomcat上运行的JSP中使用的JDBC通过JDBC访问MySQL数据库,并且遇到“未找到驱动程序异常”的问题。 我有一个方法: 这会在“ 这是我从catalina.out的打印输出”上引发错误: 唯一重要的错误是一个

  • 我总是在数据库中发现“没有合适的驱动程序”,我无法确定connect类出了什么问题?有人帮忙吗?我已经把5个罐子都装进去了。我想连接到Microsoft Access数据库。

  • 连接到sqlite数据库时遇到问题。在我的Windows PC上,一切正常,但如果我想在Linux(Raspberry PI)上运行我的程序,我会得到错误,如标题所示。 这是我在Windows上的连接: 在为Linux导出程序之前,我更改了连接字符串: 如果没有数据库,我使用以下代码创建它: 这是我的构建路径 这是我的项目 谁能帮帮我吗?THX

  • 问题内容: 我正在尝试编写一个程序以在eclipse中连接到MySQL数据库,但出现错误 “ java.sql.SQLException:找不到合适的驱动程序” 。 Java代码是: } 控制台选项卡中的输出为: 我已经使用了MySQL Connector / J。将其解压缩到MySQL安装目录中,并将jar文件添加到CLASSPATH中。 另请参阅此图像。有一个 !在项目根目录上标记。image

  • 所以我有一个在我localhost的端口8080上运行的Spring Boot应用程序。它连接到Azure sqlserver数据库并从该数据库中提取或插入项目。本地一切正常。现在我正在尝试对应用程序进行文档化。我的Dockerfile如下所示: Dockerfile位于根文件夹中,与pom的文件夹相同。xml和mssql-jdbc-9.2.1。jre15.jar文件。此jar是从Microsof