Click here to Skip to main content
14,301,527 members
Rate this:
Please Sign up or sign in to vote.
See more:
i need to connect sql server on android app with including login credentials that includes ip address ,username,password .
here i have multiple database so i need to choose one of the db and work on it !
please help me to overcome this problem !

here i attached my code to connect sql server

What I have tried:

import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;



 public class CONNECT
{
        public static void main(String[] args)
        {

            String connectionUrl =
                    "jdbc:sqlserver://104.211.212.42:1433;databaseName=CONN;integratedSecurity=false";

            String databaseUserName = "admin ";
            String databasePassword = "m";

            try (

                 Connection con = DriverManager.getConnection(connectionUrl, databaseUserName, databasePassword);
                 Statement stmt = con.createStatement())
            {
                String SQL = "SELECT Code,Name FROM COMPANY;";
                ResultSet rs = stmt.executeQuery(SQL);
                while (rs.next())
                {
              String C =   rs.getString("Code");
              String CN = rs.getString("Name");
              System.out.println(C +"-"+ CN);
                }
            }
            catch (SQLException  e)
            {

                System.out.println("Login Failed");
            }

        }

}
Posted
Updated 20-Aug-19 4:08am
v2
Comments
MadMyche 19-Aug-19 14:28pm
   
What exactly is the problem when this is executed?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
System.out.println("Login Failed");


How much do you expect to learn about your current situation from the above message when there is more (error) information available? You should learn proper coding "patterns".

Step 3: Proof of concept connecting to SQL using Java - SQL Server | Microsoft Docs[^]
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100