Click here to Skip to main content
15,896,547 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is the code for Database connection

Java
private String user = "sa";
    private String password = "hello@12345";
    private String database = "test";
    private String server = "192.168.1.89";
    private String ConnectionURL;

connectionHelper(user,password,database,server);

private Connection connectionHelper(String user, String password, String database, String server){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);

        try
        {
            try {
                Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                    + "databaseName=" + database + ";user=" + user
                    + ";password=" + password + ";";

            try
            {
                connection = DriverManager.getConnection(ConnectionURL);
                if( connection != null )
                {
                    Log.d("TAG", "Connected To SQLServer");
                }
                else {
                    Log.d("TAG", "Error in Connecting To SQLServer");
                }
            } catch (SQLException e) {
       e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return connection;
    }
Posted
Updated 30-Sep-15 22:37pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900