首先添加sql需要的包。
第一步index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is my JSP page.time now.
<br>
<form action="Login" method="get">
用户名:<input type="text" id="username" name="username" /> <br>
密码: <input type="text" id="password" name="password" /> <br> <input
type="submit" value="提交" />
</form>
</body>
</html>
第二步servlet
import java.sql.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Login
*/
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html;charset=utf-8");
// 浏览器默认的编码是iso-8859-1转换成utf-8,如果不转换,会产生乱码。
String name = new String(username.getBytes("iso-8859-1"), "utf-8");
String pass = new String(password.getBytes("iso-8859-1"), "utf-8");
System.out.println("username:" + name);
System.out.println("password:" + pass);
//如果登陆名是张三,密码是123,登陆成功,否则登陆失败。
if ("abc".equals(name)&&pass.equals("123")) {
response.getWriter().write("登陆成功");
} else
response.getWriter().write("用户名或密码错误");
//加载数据库
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test";// test为你的数据库名
String DBuserName = "sa";// 你的数据库用户名
String DBuserPwd = "123456";// 你的密码
try {
Class.forName(driverName);
System.out.println("加载驱动成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("加载驱动失败!");
}
try {
Connection dbConn = DriverManager.getConnection(dbURL, DBuserName, DBuserPwd);
System.out.println("连接数据库成功!");
// 创建statement类对象,用来执行SQL语句
Statement stat = dbConn.createStatement();
stat.executeUpdate("Insert into Xuxing values (925,'dddd','ssssssddddddddddddddddf')");
int DBid=898;
String DBintro="sd20200528omd";
String DBtitle="ccc";
// Insert into Xuxing values ("+DBid+","+DBtitle+","+DBintro+")
String sql = " Insert into Xuxing values ("+DBid+",'"+DBtitle+"','"+DBintro+"')";
stat.executeUpdate(sql);
System.out.println(sql);
System.out.println("==========================================");
// String sql = "select * from employees where name='"+username+"' and password= '"+psw+"'";
dbConn.close();// 关闭通道
System.out.print("SQL Server 命令执行完毕!");
} catch (Exception e) {
e.printStackTrace();
System.out.print("SQL Server连接失败!");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Android studio 42 服务器端 java tomcat sql 注册功能实现
最新推荐文章于 2022-01-08 19:52:36 发布