首先添加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 註冊功能實現
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.