JDBC connection to SQL Server database encountered 404, with the following issues
Attention should be paid to checking if the login user and password for your local SQL server are correct
1. Choose SQL server and Windows authentication mode
2. Select Security - Login Name - sa
3. Simply change the password
Here is the detailed code and results of my JDBC connection to SQL Server database.
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.Statement" %>
<%@page import="java.sql.ResultSet"%>
<% @page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<%
//1. three interfaces
Connection conn=null;
Statement stmt=null;
ResultSet rs =null;
//2. load driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//3. create connection object
String url="jdbc:sqlserver://localhost:1433; DatabaseName=school";
//create a username and password for logging into the database
String user ="sa";
String pwd="123456";//database login name and password required
conn = DriverManager.getConnection(url, user, pwd);
//4. create operational database objects
stmt=conn.createStatement();
//execute query
String sql="select *from student";
rs = stmt.executeQuery(sql);
//loop traversal
while(rs.next()){
//create row data in a table
%>
<tr>
<td> <%=rs.getInt(1) %></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getInt(3) %></td>
</tr>
<%
}
//close object
rs.close();
stmt.close();
conn.close();
%>
</table>
</body>
</html>
If you need to be more organized, you can modify it yourself.