Java connection to the SQL Server database, SQL Server

Source: Internet
Author: User

Java connection to the SQL Server database, SQL Server

Operating System: windows 10 64-bit

Java Development Environment: MyEclipse

Database: SQL Server 2012

JDBC driver package

Download sqljdbc4.jar

Import to project

JAVA code

 

1 package com. cl. util; 2 3 import java. SQL. connection; 4 import java. SQL. driverManager; 5 import java. SQL. preparedStatement; 6 import java. SQL. resultSet; 7 import java. SQL. SQLException; 8 9 public class DButil {10 11 // connection DRIVER 12 private static final String DRIVER = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; 13 // connection path 14 private static final String URL =" jdbc: sqlserver: // localhost: 1433; databas EName = master "; 15 // USERNAME 16 private static final String USERNAME =" sa "; 17 // PASSWORD 18 private static final String PASSWORD =" 123 "; 19 20 // static code block 21 static {22 try {23 // load drive 24 Class. forName (DRIVER); 25} catch (ClassNotFoundException e) {26 e. printStackTrace (); 27 System. out. println ("database Connection exception"); 28} 29} 30 31/* 32 * Get database Connection 33 */34 public static Connection getConn () {35 Connection conn = nu Ll; 36 try {37 conn = DriverManager. getConnection (URL, USERNAME, PASSWORD); 38} catch (SQLException e) {39 e. printStackTrace (); 40} 41 return conn; 42} 43 44/* 45 * close the database connection. Pay attention to the sequence of closing: 46 */47 public void close (ResultSet rs, PreparedStatement ps, connection conn) {48 if (rs! = Null) {49 try {50 rs. close (); 51 rs = null; 52} catch (SQLException e) {53 e. printStackTrace (); 54} 55} 56 if (ps! = Null) {57 try {58 ps. close (); 59 ps = null; 60} catch (SQLException e) {61 e. printStackTrace (); 62} 63} 64 if (conn! = Null) {65 try {66 conn. close (); 67 conn = null; 68} catch (SQLException e) {69 e. printStackTrace (); 70} 71} 72} 73}
Note:
Localhost: 1433
1433 is the default port number of SQL server. You can modify the port number in special cases.


Some security guards may prompt for accelerated startup and disable SQL services.
Remember to open

Locate SQL Server (MSSQLSERVER) and start the service

 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.