Java code for connecting MS SQL Server by using SQL Server Authentication
First of all, You will need to add a jar file to your project library as SQL Server 2000 Driver for JDBC Service. My target is SQL Server 2000, it will require the jar file called "sqljdbc4.jar". This is not supported on Microsoft website now, you can download it here. For other versions of SQL Server, here is the link of SQL Server 2000 Driver for JDBC Service.
The following is the code for connection MS SQL Server and select some records from a testing table.
import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection("jdbc:sqlserver://HOSP_SQL1.company.com;user=name;password=abcdefg;database=Test"); System.out.println("test"); Statement sta = conn.createStatement(); String Sql = "select * from testing_table"; ResultSet rs = sta.executeQuery(Sql); while (rs.next()) { System.out.println(rs.getString("txt_title")); } } } |
<pre><code> String foo = "bar"; </code></pre>
-
Rahul Yadav
-
kallo
-
pandeyji
-
Apalna
-
nacho
-
aaru
-
Apalna
-
sdasd
-
aaru
-
lo kallo bt
-
Apalna
-
mekoBnipta
-
KyaPtaYr
-
jai
-
tozduman
-
Guest
-
rajendra lamror
-
suresh
-
Slava Gorbunov
-
Slava Gorbunov
-
sana