get SQL Connection to SQLServer database - Android Database

Android examples for Database:Database Connection

Description

get SQL Connection to SQLServer database

Demo Code


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.os.Bundle;
import android.os.Message;
import android.widget.TextView;

public class Main{
    final static String user = "sa";
    final static String pwd = "";
    final static String db = "LocomtiveOverMonitor";
    public static Connection getSQLConnection() {
        Connection con = null;/*from ww  w. j  av  a 2s .  co m*/
        try {
            if (!LoginActivity.TestConnect())
                return null;
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            DriverManager.setLoginTimeout(1);
            con = DriverManager.getConnection("jdbc:jtds:sqlserver://"
                    + LoginActivity.strServerIP + ":1433/" + db
                    + ";charset=utf8", user, pwd);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
}

Related Tutorials