A Practical Tutorial for JDBC Using IDEA to Connect to SQLServer Database

Table of contents

foreword

1. Database preparation

1. Open the TCP/IP protocol through the SQLServer Configuration Manager, and set the status to Enabled

2. The database login mode is set to authentication login (if it has already been set, this step can be ignored)

3. Download the SQLServer driver jar package

2. IDEA preparation

1. Import the SQLServer driver jar package

 2. Write the test code and initiate a SQL statement request to the SQLServer database

 3. Connection error solution

Four. Summary


foreword

Recently, after learning Java and JDBC, I also found articles from many bloggers on the Internet about connecting to the database. During this period, I also encountered many problems, and finally solved the database connection problem after some twists and turns. I also understand that novices are very troublesome when they come into contact with JDBC, especially when establishing a database connection. For this reason, the blogger spent an hour and a half at night, carefully preparing this tutorial to answer.

1. Database preparation

1. Open the TCP/IP protocol through the SQLServer Configuration Manager, and set the status to Enabled

Follow the steps: This Computer --> Management --> Services and Applications --> SQLServer Configuration Manager --> SQLServer Network Configuration

-->Choose the protocol to open your local SQLServer instance -->TCP/IP protocol

Note: If the TCP/IP protocol has been disabled, you need to right-click to select Open and set it to the enabled state, otherwise it will affect the subsequent database connection operations! Restart the SQLServer service after enabling it: Open the SQLServer service in the SQLServer Configuration Manager, right-click SqlServerAnalysisServices (yellow icon) to restart, and wait patiently to complete the configuration just now.

Double-click the IP address --> IPALL (pull down to the end) --> change the TCP port to 1433 (1433 is the default port number of the SQLServer database, and different databases have different default port numbers)

2. The database login mode is set to authentication login (if it has already been set, this step can be ignored)

Open the database instance --> Security --> Login Name --> Right-click sa --> Properties --> Check SQLServer authentication and set a password

Open the state again, grant the connection to the database engine and enable the login

(Note: This is generally the default, but it does not rule out that rejection and disabling factors are set when installing the SQLServer database, so check to ensure that the final connection can be successful)

 Then right-click the database instance to open the server properties, open the security, the configuration is as follows

 Restart the database to make the above configuration take effect.

(Close and reopen or right-click the server and click to restart the database service)

3. Download the SQLServer driver jar package

Baidu network disk has been uploaded:

Link: https://pan.baidu.com/s/1oYYEzXJt-22fKssHl4C7Ag 
Extraction code: o999

(Note: There are three driver packages in it, readers can choose according to their own SQLServer version, I am currently using SQLServer2012)

2. IDEA preparation

1. Import the SQLServer driver jar package

Files in the upper left corner-->Project Structure-->Libraries-->click the + sign-->select Java-->select the path of the SQLServer driver package you just downloaded-->OK

 2. Write the test code and initiate a SQL statement request to the SQLServer database

The following test code is established in the database. A database named school has been established, and a student table is created in it

 Test code:

import java.sql.*;
public class sql2 {
    public static void main(String args[]){
            Connection con;
            Statement st;
            ResultSet rs;
            String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=school";
            String userName="sa";
            String userPwd="123456";
            String sql="select * from student";
            try {
                con = DriverManager.getConnection(dbURL, userName, userPwd);
                st = con.createStatement();
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    String sno = rs.getString(1);
                    String sname = rs.getString(2);
                    String ssex = rs.getString(3);
                    String sbir = rs.getString(4);
                    String sclass = rs.getString(5);
                    System.out.println("学号:" + sno + "\n姓名:" + sname + "\n性别:" + ssex
                            + "\n出生日期:" + sbir + "\n班号:" + sclass);
                }
                rs.close();
                st.close();
                con.close();
            }catch(SQLException e){
                e.printStackTrace();
                System.out.println("数据库连接失败!!!");
            }

    }
}

operation result:

If you can print out the data queried from the database, congratulations, the database has been successfully connected!

 3. Connection error solution

Please learn from this blogger’s article, it’s very detailed

2 solutions

Four. Summary

        As a programmer, the ability to solve problems by yourself is very important, the so-called Debug ability. There is a popular saying on the Internet: "Programmers work three times in a row: check bugs, fix bugs, write bugs...", of course this is a joke, but it is also very realistic.

        Don't panic when you encounter problems, and have the courage to explore and solve them. Just like this database connection problem, I encountered many problems at the beginning, and finally solved it after twists and turns, and successfully helped two roommates successfully connect to the database, and it was done in about ten minutes.

        So only if you work hard can you look effortless. As said!

-----------------------------------------

Finally, if you think this article is helpful to you, don't forget to like and pay attention.

If there are still problems with the connection, you can private message me, and I will reply when I have time.

Guess you like

Origin blog.csdn.net/qq_52487066/article/details/124787906