eclipse connect to sql server2019 database

Package installation

Insert picture description here


First download the corresponding package on Microsoft's official website . After
downloading, find the path of the java you installed, and unzip the entire file just downloaded to

C:\Program Files\AdoptOpenJDK\jdk-11.0.10.9-hotspot\lib

Call related packages

Insert picture description here


After opening, the following interface is

Insert picture description here


displayed. After selecting Modulepath, you can select Add Extemal JAR on the right button...

Insert picture description here


Then find the file you just unzipped, find the chs folder, and select different jar archives according to different versions.

Code test

package sql;

import java.sql.*;
import java.util.Scanner;

public class sql {
	public static void main(String[] args) 
    {
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		// 加载JDBC驱动
		String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=study";
		// 连接服务器和数据库
		String userName = "sa"; // 默认用户名
		String userPwd = "password"; // 密码
		Connection connect;
		
        
        try 
        {  
               System.out.println("start connecting");
               Class.forName(driverName);
               connect = DriverManager.getConnection(dbURL, userName, userPwd);
          
               System.out.println("Success connect sql server!");  
               
               Statement stmt = connect.createStatement();  

               //查询
               ResultSet rs = stmt.executeQuery("select * from student");  

               while (rs.next()) 
               {  
                   
                   String uid = rs.getString("sno");
                   String name = rs.getString("sname");
                   String age = rs.getString("sage");
                   
                   System.out.println("学号:" + uid +""
                        + "\t" + "姓名:" + name + "\t" + "年龄:"+ age + "\n" );
               }   
               //插入
               System.out.println("请输入要插入的学号,姓名,和年龄");
               Scanner scanner = new Scanner(System.in); 
               String myid = scanner.nextLine();
               String myname = scanner.nextLine();
               String myage = scanner.nextLine();
               PreparedStatement prepStmt = connect.prepareStatement("insert into student values(?,?,?)");
               prepStmt.setString(1, myid);
               prepStmt.setString(2, myname);
               prepStmt.setString(3, myage);
               prepStmt.executeUpdate();
               System.out.println("插入成功!");
               System.out.println("现在的表为:");
               Statement stmt2 = connect.createStatement();  
               
               ResultSet rs2 = stmt2.executeQuery("select * from student");  

               while (rs2.next()) 
               {  
                   
                   String uid = rs2.getString("sno");
                   String name = rs2.getString("sname");
                   String age = rs2.getString("sage");
                   
                   System.out.println("学号:" + uid +""
                        + "\t" + "姓名:" + name + "\t" + "年龄:"+ age + "\n" );
               }   
               //删除
               
               System.out.println("请输入要删除的学号");
               Scanner scanner2 = new Scanner(System.in); 
               String myid2 = scanner2.nextLine();
               PreparedStatement prepStmt2 = connect.prepareStatement("delete from student where sno=?");
               prepStmt2.setString(1, myid2);
               prepStmt2.executeUpdate();
               
               System.out.println("删除成功!");
               System.out.println("现在的表为:");
               
               Statement stmt3 = connect.createStatement();  
               
               ResultSet rs3 = stmt3.executeQuery("select * from student");  

               while (rs3.next()) 
               {  
                   
                   String uid = rs3.getString("sno");
                   String name = rs3.getString("sname");
                   String age = rs3.getString("sage");
                   
                   System.out.println("学号:" + uid +""
                        + "\t" + "姓名:" + name + "\t" + "年龄:"+ age + "\n" );
               }   

               System.out.println("请输入要修改的学号,并输入修改后的姓名和年龄");
               Scanner scanner3 = new Scanner(System.in); 
               String myid3 = scanner3.nextLine();
               String myname3 = scanner3.nextLine();
               String myage3 = scanner3.nextLine();
               PreparedStatement prepStmt3 = connect.prepareStatement("UPDATE student SET sname = ?, sage = ? WHERE sno = ?");
               prepStmt3.setString(1,myname3);
               prepStmt3.setString(2,myage3);
               prepStmt3.setString(3,myid3);
               prepStmt3.executeUpdate();
               System.out.println("修改成功!");
               System.out.println("现在的表为:");
               
               Statement stmt4 = connect.createStatement();  
               
               ResultSet rs4 = stmt4.executeQuery("select * from student");  

               while (rs4.next()) 
               {  
                   
                   String uid = rs4.getString("sno");
                   String name = rs4.getString("sname");
                   String age = rs4.getString("sage");
                   
                   System.out.println("学号:" + uid +""
                        + "\t" + "姓名:" + name + "\t" + "年龄:"+ age + "\n" );
               }   
               
               prepStmt.close();
               prepStmt2.close();
               prepStmt3.close();
               
               rs.close(); 
               rs2.close();
               rs3.close();
               rs4.close();
               stmt2.close();
               stmt3.close();
               stmt4.close();
               stmt.close();
               connect.close(); // 关闭连接  
         }  
         catch (Exception e) 
         {  
              System.out.print("get data error!");  
              e.printStackTrace();  
          }  
    }  

}