Sql Database connect Using the JDBC Driver with android

前端 未结 1 1948
星月不相逢
星月不相逢 2020-12-18 17:47

I\'m trying to connect to SQL Serverusing JDBC in an android app. I imported sqljdbc4.jar into my application but when i get to

conn = DriverManager.

相关标签:
1条回答
  • 2020-12-18 18:29

    Change the Dirver Name and before import the lib file jtds 1.3.0.jar file.and change below the code it will work fine.............

    public List<String> dbConnect(String Host, String Port, String db_userid,
                    String db_password) {
                List<String> Db_list = new ArrayList<String>();
                try {
                    String ConnectionString = "jdbc:jtds:sqlserver://" + Host + ":"
                            + Port;
                    // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
                    Connection conn = DriverManager.getConnection(ConnectionString,
                            db_userid, db_password);
                    System.out.println("connected");
                    Statement statement = conn.createStatement();
                    String queryString = "select name from sys.databases";
                    ResultSet rs = statement.executeQuery(queryString);
                    while (rs.next()) {
                        Db_list.add(rs.getString(1));
                    }
                } catch (Exception e) {
                    Db_list.add("Error");
                    e.printStackTrace();
                }
                return Db_list;
            }
    
    0 讨论(0)
提交回复
热议问题