Java connection Simple database application for SQL Server 2008

Source: Internet
Author: User
Tags microsoft sql server stmt management studio microsoft sql server management studio sql server management sql server management studio

1. Download the JDBC driver package from Microsoft Official website

Sqljdbc_4.0.2206.100_chs.exe, double-click Unzip file to the specified directory, my specified directory is:

C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\chs

Here is the extracted file,

If the JDK version is less than 6.0, you need to use Sqljdbc.jar;

6.0 or later use Sqljdbc4.jar, use Sqljdbc4.jar version benefits can be omitted

Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver") this section of code.

2. Add the file path where the Sqljdbc4.jar is located in the system environment variable classpath;

3. Open SQL Server 2008 Configuration Manager (SQL Server Profile Manager)

Start the TCP/IP port and right-click the Select attribute to set the TCP address port to 1433 (the port value can be defined within 0~65535);

3. Open the DOS command line interface and enter the NETSTAT-A command to see which ports are open:

Netstat-a See which ports are open
Netstat-n Viewing the network connectivity of ports
Netstat-v viewing work in progress
Netstat-p TCP/IP viewing of a protocol usage
Netstat-s View all protocol usage in use

4. Connect to the database:

(1) Connect to the database with Windows authentication: This approach may require administrator privileges, at least in some cases, to prevent the database from being accessed properly. It is not recommended to use!

(2) Connect the database with SQL Server authentication.

If you installed SQL Server 2008 with Windows authentication and did not add a SQL Sever identity user for SQL Server 2008, you need to add the user first:

Open Microsoft SQL Server Management Studio and log on as Windows authenticated, on the left, Object Explorer, security-login, right-click the Sa-> property, add a password for the SA user, select SQL Server authentication, which is granted in the "status" item to connect to the database and login enabled;

Right-click the root node of the Object Explorer, select Properties---Security->sql server and Windows Authentication mode, and then

A user SA was created with SQL Server authentication.

The SA user password I created is "12345";

5. Connect the Java code for the SQL Server 2008 database:

Connect Database code:

String url = "JDBC:SQLSERVER://LOCALHOST:1433;DATABASENAME=TEST1;";

conn = drivermanager.getconnection (URL, "sa", "12345");

Full code:

Before you connect to the database with Java, create a database or import a database, and then Java connects and accesses the database!

1  Packagejdbc_test;2 3 Importjava.sql.SQLException;4 Importjava.sql.Statement;5 Importjava.sql.Connection;6 ImportJava.sql.DriverManager;7 ImportJava.sql.ResultSet;8 9  Public classTest {Ten      Public Static voidMain (string[] args) { One Connection Conn; A Statement stmt; - ResultSet rs; -String url = "Jdbc:sqlserver://localhost:1433;databasename=student_course;"; theString sql = "SELECT * FROM Student"; -         Try { -             //connecting to a database -conn = drivermanager.getconnection (URL, "sa", "12345"); +             //Creating statement Objects -stmt =conn.createstatement (); +             /** A * Statement createstatement () creates a Statement object to send SQL statements to the database.  at              */ -             //Executing database query statements -rs =stmt.executequery (SQL); -             /** - * ResultSet executeQuery (String sql) throws SQLException executes the given SQL - * statement, which returns a single ResultSet object in              */ -              while(Rs.next ()) { to                 intid = rs.getint ("Sno"); +String name = rs.getstring ("Sname"); -                 intAge = Rs.getint ("Sage"); theSystem.out.println ("Sno:" + ID + "\tsame:" + name + "\tsage:" +Age ); *             } $             if(rs! =NULL) {Panax Notoginseng rs.close (); -rs =NULL; the             } +             if(stmt! =NULL) { A stmt.close (); thestmt =NULL; +             } -             if(Conn! =NULL) { $ conn.close (); $conn =NULL; -             } -}Catch(SQLException e) { the e.printstacktrace (); -SYSTEM.OUT.PRINTLN ("Database connection Failed");Wuyi         } the     } -}

The result is:

6. Test the required database connection:

Link: Http://pan.baidu.com/s/1nvRPKfR Password: 3vms

Java connection Simple database application for SQL Server 2008

Related Article

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.