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"
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 .
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.
The Microsoft JDBC Driver 11.2 for SQL Server provides mssql-jdbc-11.2.
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!
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>"); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With