Introduction to Java connection to SQL Server under Windows system

  • 2020-04-01 04:07:54
  • OfStack

Connect to SQL Server using JDBC

Set up the SQL Server Server

I used SQL Server 2005 standard version SP2, these are the default, generally do not need to configure. If you need to configure ports, see below.

1, "start" - "program" - "Microsoft SQL Server 2005" - "configuration tools" - "SQL Server configuration manager" - "SQL Server 2005 network configuration" - "MSSQLSERVER protocol"
2. If TCP/IP is not enabled, right-click and select start.
3. Double-click "TCP/IP" to enter the property setting. In "IP address", "TCP port" in "IPAll" can be configured.
4. Restart SQL Server or restart the computer.


Create a database

Open "SQL Server Management Studio", log in to connect to the SQL Server Server, create a new database, and name it test


Test in Eclipse

1. Open Eclipse, "file" - "new" - "project" - "Java project", the project name is Test
2. In Eclipse, select "window" - "preferences..." - "Java" - "installed JRE", select the installed JRE, click "edit" - "add external", select %ProgramFiles%\sqljdbc_1.1\ CHS \sqljdbc.jar
3. Sqljdbc.jar can be seen in the "JRE system library" of the Test project. If not, right-click the project Test - "build path" - "configure build path..." - "Java build path" - "library" - "add external JAR..." , select the % ProgramFiles % \ sqljdbc_1 1 \ CHS \ SQLJDBC jar
4. Write Java code as follows:


import java.sql.*;
public class Test {
public static void main(String[] srg) {
 String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //Loading JDBC driver
 String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=test"; // Connect to server and database test
 String userName = "sa"; //Default user name
 String userPwd = "123456"; //password
 Connection dbConn;
 try {
  Class.forName(driverName);
  dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
  System.out.println("Connection Successful!"); //Console outputs Connection Successful if Connection succeeds!
 } catch (Exception e) {
  e.printStackTrace();
 }
}
}


Note:
1. Because the server of this version of SQL Express is disabled by default and the port number is not configured, it needs to be reset
2. If you used to connect to SQL Server 2000 with Java, please note:
The statement that loads the driver and URL path in SQL Server 2000 is
String driverName = "com. Microsoft. JDBC. Essentially. SQLServerDriver";
String dbURL = "JDBC: Microsoft: essentially: / / localhost: 1433; DatabaseName = sample ";
The statement that loads the driver and URL in SQL Server 2005 is
String driverName = "com. Microsoft. Essentially. JDBC. SQLServerDriver";
String dbURL = "JDBC: essentially: / / localhost: 1433; DatabaseName = sample ";
The driver will not be found if it is written incorrectly.


JTDS connect is essentially
JTDS is an open source 100% pure Java driver for JDBC 3.0 drivers for Microsoft SQL Server and Sybase (versions 10, 11, 12, 15). JTDS is based on freetds and is currently the fastest production prepared JDBC driver for SQL Server and Sybase.
JTDS is fully JDBC 3.0 compatible, supports parallel (fully independent) statements in forward-only, scrollable/updatable ResultSets, and implements all databasemetadata and resultsetmetadata methods.
JTDS - SQL Server and Sybase JDBC driver
 


package sqlserver_jtds; 
 
import java.sql.*; 
 
public class SQLServer { 
 String dbURL = "jdbc:jtds:sqlserver://127.0.0.1:1433;;DatabaseName=test"; 
 String user = "sa"; 
 String password = "123456"; 
  
 Connection conn; 
  
 public SQLServer(){ 
 this.connect(); 
 } 
 public void connect(){ 
 try{ 
  try{ 
  Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
  }catch(Exception e){ 
  e.printStackTrace(); 
  } 
  //DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver()); 
  conn = DriverManager.getConnection(dbURL,user,password); 
  DatabaseMetaData metaData = conn.getMetaData(); 
   
  System.out.print(metaData.getDatabaseProductVersion()); 
 }catch(Exception e){ 
  e.printStackTrace(); 
 } 
 } 
 public static void main(String[] args){ 
 new SQLServer(); 
 } 
} 


Related articles: