Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to a SQL Server 2008 database using JDBC?

I have MSSQL 2008 installed on my local PC, and my Java application needs to connect to a MSSQL database. I am a new to MSSQL and I would like get some help on creating user login for my Java application and getting connection via JDBC. So far I tried to create a user login for my app and used following connection string, but I doesn't work at all. Any help and hint will be appreciated.

jdbc:jtds:sqlserver://127.0.0.1:1433/dotcms  username="shuxer"  password="itarator" 
like image 553
taras Avatar asked Mar 16 '10 03:03

taras


People also ask

How do I connect to a SQL Server database using JDBC?

Connect to SQL Server Using JDBC Driver and Command Linedatasource = "MSSQLServerAuth"; username = ""; password = ""; conn = database(datasource,username,password); Or, to connect without Windows authentication, use the configured JDBC data source and specify the user name username and the password pwd .

What is JDBC URL for SQL Server?

jdbc:sqlserver:// (Required) is known as the subprotocol and is constant. serverName (Optional) is the address of the server to connect to. This address can be a DNS or IP address, or it can be localhost or 127.0. 0.1 for the local computer.

What is the JDBC driver name for SQL Server?

The Microsoft JDBC Driver 11.2 for SQL Server provides mssql-jdbc-11.2.


2 Answers

There are mainly two ways to use JDBC - using Windows authentication and SQL authentication. SQL authentication is probably the easiest. What you can do is something like:

String userName = "username"; String password = "password";  String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB";  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection(url, userName, password); 

after adding sqljdbc4.jar to the build path.

For Window authentication you can do something like:

String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection(url); 

and then add the path to sqljdbc_auth.dll as a VM argument (still need sqljdbc4.jar in the build path).

Please take a look here for a short step-by-step guide showing how to connect to SQL Server from Java using jTDS and JDBC should you need more details. Hope it helps!

like image 56
Thusi Avatar answered Sep 24 '22 03:09

Thusi


You can use this :

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;  public class ConnectMSSQLServer {    public void dbConnect(String db_connect_string,             String db_userid,             String db_password)    {       try {          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");          Connection conn = DriverManager.getConnection(db_connect_string,                   db_userid, db_password);          System.out.println("connected");          Statement statement = conn.createStatement();          String queryString = "select * from sysobjects where type='u'";          ResultSet rs = statement.executeQuery(queryString);          while (rs.next()) {             System.out.println(rs.getString(1));          }       } catch (Exception e) {          e.printStackTrace();       }    }     public static void main(String[] args)    {       ConnectMSSQLServer connServer = new ConnectMSSQLServer();       connServer.dbConnect("jdbc:sqlserver://<hostname>", "<user>",                "<password>");    } } 
like image 22
Mahmut EFE Avatar answered Sep 23 '22 03:09

Mahmut EFE