简体   繁体   中英

Error While connecting to SQL server in android application using eclipse

I am finding exception on "conn = DriverManager.getConnection(connString, username, password); Line.

public void query2() {
    Log.i("Android", " MySQL Connect Example.");
    Connection conn = null;
    try {
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        System.out.println("*************inside class 1**************");
        Class.forName(driver).newInstance();

       // test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
        System.out.println("*************inside class 2**************");
        String connString = "jdbc:jtds:sqlserver://localhost:1433;" +
                "DatabaseName=test;user=sa;password=sa1;";
        System.out.println("*************inside class 3**************");
        String username = "sa";
        String password = "sa1";
        conn = DriverManager.getConnection(connString, username, password);
        Log.w("Connection", "open");
        Statement stmt = conn.createStatement();
        ResultSet reset = stmt.executeQuery("select * from titles");

        // Print the data to the console
        while (reset.next()) {
            Log.w("Data:", reset.getString(3));
            // Log.w("Data",reset.getString(2));

        }
        conn.close();

    } catch (Exception e) {
        Log.w("Error connection", "" + e.getMessage());
    }
}

My Log cat display:

  02-01 06:42:36.845: I/Android(1187):  MySQL Connect Example.
  02-01 06:42:39.365: I/System.out(1187): *************inside class 1**************
  02-01 06:42:40.843: I/System.out(1187): *************inside class 2**************
  02-01 06:42:42.396: I/System.out(1187): *************inside class 3**************
  02-01 06:42:45.220: D/dalvikvm(1187): GC_CONCURRENT freed 161K, 10% free 2606K/2888K, paused 74ms+72ms, total 233ms
  02-01 06:43:00.970: W/Error connection(1187): null                                        

Your connection string looks malformed. Also MySQL default port is 3306. Unless you changed the port number you should use 3306. Pull the username and passwrod out of the Connstring. If you are using

getConnection (ConnStr, User, Password) 

you don't need to set it in the conn string as you're passing it in the call.

Try this conn string which is bare bones but should connect. Make sure to use getConnection and pass in username/password

jdbc:jtds:sqlserver://localhost:3306/test

If this does not work you will need to output more info and errors to Logcat. Look here to understand how to use Logcat. You don't have any error messages in Logcat in your Post, only Info and Warning

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM