Step 1: configure SQL Server (2016), log in to SQL Server under windows, right-click sa under the security login name, and enter the property change. Then, uncheck the enforce password policy to modify the password.
After the password is modified, log out and log in again using the account with the user name sa.
Step 2: right click the login name to enter the configuration login option:
Select security, check SQL server and windows authentication mode. At the same time, under connections, check allow remote connections to the server.
Step 3: configure Eclipse. To open the program, first we need to load the SQL server driver. Download on Microsoft official website: http://www.microsoft.com/zh-cn/download/details.aspx?id=11774 . Click download exe file. After the file is downloaded successfully, double-click uzip, extract the file, find the file in this path, and we will apply the file.
Then, open Eclipse, create a new java project, right-click the configuration build path under the build path. As shown in the picture:
Click add external jar, find the jar file under the directory jre8 you just installed, and then add it.
The fourth step is to write the code. Before writing the code, we need to establish a database, which is as follows:
The code is as follows:
package hotel;
import java.sql.*;
public class test {
public static void main(String[] args) throws InstantiationException,
IllegalAccessException,ClassNotFoundException{
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//The URL points to the database name student to be accessed
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=student";
//User name for SQL server Configuration
String user = "sa";
//Password for SQL server Configuration
String password = "123";
try {
Class.forName(driver);}
catch(ClassNotFoundException e) {}
try {
Connection con;
con = DriverManager.getConnection(url,user,password);
ResultSet rs=null;
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select sname from teacher where sex='female'");
while(rs.next()) {
System.out.println(rs.getString("sname"));
}
String sql="insert into teacher(sno,sname,sex,sdept)values('160730222','Xiao Wu','female','Internet of things')";
stmt.executeUpdate(sql); //Perform insert operation
String sql1="update teacher set sname ='Xiao Liu' where sno =160730222";
stmt.executeUpdate(sql1); //Perform update operation
String sql2="delete from teacher where sname='Xiao Liu'";
stmt.executeUpdate(sql2);// Perform delete operation
rs.close();
stmt.close();
con.close();
}
catch(SQLException e) {
System.out.println(e);
}
}
}
The corresponding port number of my computer IP is 1433, which needs to be modified according to my own computer. The specific query method is no longer redundant. Send the website to self search: TCP native query method . After compiling, it is found that the connection of the database is successful. You can annotate the insert, update, and delete operations respectively. Watch the database phenomenon.