strange_098
strange_098

Reputation: 1391

How to connect Java to Microsoft SQL server

I made a Java application to connect to a MySQL database. The connection was made ​​in this way:

public class Connection {

    public static Connection getConexao() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //System.out.println("Conectado");
            return DriverManager.getConnection("jdbc:mysql://localhost/world","root", "rootadmin");
        } catch (ClassNotFoundException e) {
            throw new SQLException(e.getMessage());
        }
    }
}

Now I needed to change the connection from MySQL to Microsoft SQL Server 2012.

Can anyone help me change the connection to the database?

Thank you all very much.

Upvotes: 0

Views: 10077

Answers (3)

Fevly Pallar
Fevly Pallar

Reputation: 3109

This answer is presented for next visitors on this kind of question. Configuring the java driver connection for SQL Server can be quite confusing for new users. I'll guide you here through SQL Server Management Studio (SSMS):

There're 2 kinds of authentification accepted on SQL Server. They are Windows & SQL Server authentification. In this answer I'll active "sa" (syst. administrator) account for quick setup demonstration over the connection.

To enable the "sa" account (you can skip this if it had already been there):

  • Login as usual using default window authentification mode
  • Right click on the server name (i.e MYCOMPUTER223\SQLEXPRESS) > Security > go enable the SQL Server & Window authentification mode > ok
  • On the left tree menu, click Security > Logins > right click that "sa" > Properties > set up your "password" for this "sa" account
  • and then on the left menu there is the "Status"> enable the "Login:"
  • restart the SQL Server service
  • now login as "sa" through "SQL Server authentification mode" on the SMSS . Using the password we've just set up.

Enable the TCP/IP for the conn. instance (this is by default is disabled particularly on sql express editions):

  • Open "Sql Server Configuration Manager". This is installed along the installation of SQL Server engine.
  • "SQL Server Network Configuration" > "Protocol for SQLExpress" > enable the "TCP/IP"
  • right click that "TCP/IP" > "IP Address" > scroll down till you find "IPAll" and then just fill the "port" field with 1433

You can now use this credential for the SMSS:

username       : sa
 password      : ...the password you've just set up above..

Or you can now use this credential on your external java based clients/data or BI tools/sql management tools such as Pentaho, Heidi SQL, DB Weaver or any particular java framework conn. manager descriptor, etc. :

hostname      : localhost (or any custome host domains)
database name : your database name..
instance name : i.e SQLEXPRESS (this can be found through the SMSS, right click the server name > view connection properties)
port          : 1433
username      : sa
password      : ...the password you've just set up above..

or via url/uri for the Java connection manager/factory:

    String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=Yourdatabasename;user=sa;password=yourSApassword";

public Connection createConnection() throws NoSuchAlgorithmException {      
        System.out.println("Creating SQL Server DataBase Connection");
        Connection connection = null; 
        try {  
            // Provide the java database driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
            // Provide URL, database and credentials according to your database
            // .getConnection ("url/namadatabase, user, password")
            String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=DummyDatabase;user=sa;password=YourSAaccountpassword";
            connection = DriverManager.getConnection(Connectionurl);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (connection != null) {
            System.out.println("Connection created successfully..");
        }
        return connection;
    }

Upvotes: 2

Dharmraj
Dharmraj

Reputation: 174

Make database URL like :

jdbc:mysql://IP address:DatabasePort/DatabaseName,username, password   

public class Connection {

public static Connection getConexao()throws SQLException{
    try{
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/world","root", "rootadmin");

    }catch(ClassNotFoundException e) {
        throw new SQLException(e.getMessage());
    }
}

}

Upvotes: 0

jdiver
jdiver

Reputation: 2393

First of all you will need JDBC drivers for MS SQL Server. Either from Microsoft or there are other options like jTDS.

Then you should use a connection string like jdbc:sqlserver://ServerName:Port;databaseName=;user=username;password=password;

Of course your SQL Server should be in mixed mode so you can connect with username and password created on server.

Applets run on users' computer, therefore you should open your SQL Server ports to all visitors which is a BAD idea.

Upvotes: 1

Related Questions