IDEA sqlserver database connection

Database Configuration

Install sqlserver. Links: sqlserver database engine , management tools . Installation ...... find their own tutorial.

log-in name

1. Log on to verify with Windows
Here Insert Picture Description
2. Open the login name> security, find or create a new login name
Here Insert Picture Description
right login name, password
Here Insert Picture Description
3. Log back management tool, SQL Server authentication method, login name, password is correct then the login is successful
Here Insert Picture Description
Create a database in which to test the new people table
Here Insert Picture Description

Port Settings

1. Right My Computer -> Manage -> Services and Applications -> SQL server network configuration
Here Insert Picture Description
2. Locate the TCP / IP, double-click to open the IP Address tab, and configure
Here Insert Picture Description
Here Insert Picture Description
3. Restart the SQL Server service

Download JDBC package

Download JDBC and unzip to any location
Here Insert Picture Description

Introducing the jar package IDEA

1. Create a new project, create a test class

public class Test {
    private static Connection dbConn = null;

    public static void main(String[] args) {
        String dbURL = "jdbc:sqlserver://192.168.1.125:1433;DatabaseName=sa";

        try {
            //1.加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("加载驱动成功!");
            //2.连接
            dbConn = DriverManager.getConnection(dbURL, "sa", "123");
            System.out.println("连接数据库成功!");
            String sql="select * from people";
            PreparedStatement statement=null;
            statement=dbConn.prepareStatement(sql);
            ResultSet res=null;
            res=statement.executeQuery();
            while(res.next()){
                String title=res.getString("name");
                System.out.println(title);
            }
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("连接数据库失败!");
        }

    }
}

2. Press Ctrl + Alt + shift + S or open File-> Project Structure, open the Dependencies tab, add the following jar package
Here Insert Picture Description
3. Press Ctrl + shift + F10 to start the program, the following results

加载驱动成功!
连接数据库成功!
王五
李四
张三

Reading success

Released eight original articles · won praise 4 · Views 2973

Guess you like

Origin blog.csdn.net/yezuofenglin/article/details/100552190