JavaWeb +sql server+tomcat


JavaWeb +sql server+tomcat

 

database:

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.usedata;

public class Database {
Connection conn=null;
public Database() throws SQLException, ClassNotFoundException{

Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
this.conn=DriverManager.getConnection(“jdbc:sqlserver://localhost:1433;DatabaseName=CarData”,”sa”,”sa123456..”);
System.out.println(conn);
}
public void showConnection() {
if(conn!=null) { System.out.println(“success”);}
else {System.out.println(“fail”);}

}
//关闭连接
public void close() throws SQLException {
conn.close();
}
//插入
public void insert(String name,String pwd) throws SQLException {
PreparedStatement prep = conn.prepareStatement(“insert into [web001].[dbo].[login2] values(null,?,?)” );
prep.setString( 1, pwd);
prep.setString( 2, name);
prep.execute();
}
public ArrayList<usedata> getAlluser() throws SQLException{
ArrayList<usedata> userlist=new ArrayList<usedata>();
PreparedStatement prep = conn.prepareStatement(“select * from [web001].[dbo].[login2]” );
prep.execute();
ResultSet resultSet =prep.executeQuery();
while(resultSet.next()) {
int myid = resultSet.getInt(“id”);
String myname = resultSet.getString(“name”);
String mypwd = resultSet.getString(“pwd”);
userlist.add(new usedata(myid,myname,mypwd));
}
return userlist;
}
public usedata getUser(int id) throws SQLException {
PreparedStatement prep = conn.prepareStatement(“select * from [web001].[dbo].[login2] where id=?” );
prep.setInt( 1, id);
prep.execute();
ResultSet resultSet =prep.executeQuery();
while(resultSet.next()) {
int myid = resultSet.getInt(“id”);
String myname = resultSet.getString(“name”);
String mypwd = resultSet.getString(“pwd”);
return new usedata(myid,myname,mypwd);
}
return null;

}
}

 

Test.java

package dbTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public class test {
public void main(String[] srg) {
String driverName = “com.microsoft.sqlserver.jdbc.SQLServerDriver”;
// 加载JDBC驱动
String dbURL = “jdbc:sqlserver://localhost:1433; DatabaseName=数据库名字”;
//Database是数据库的名字,localhost后面是你SQL Server的端口号默认1433
// 连接服务器和数据库
String userName = “sa”; // 默认用户名
String userPwd = “这里输入刚刚的密码”; // 密码
Connection dbConn = null;

try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println(“Connection Successful!”);
// 如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

 

Loginservlet

package login;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* Servlet implementation class LoginSerlet
*/
@WebServlet(name=”LoginSerlet”,urlPatterns=”/login”)
public class LoginSerlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginSerlet() {
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
response.getWriter().append(“Served at: “).append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

//乱码
response.setContentType(“text/html;charset=utf-8”);
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
int id = Integer.parseInt(request.getParameter(“id”));
String pwd=request.getParameter(“pwd”);
PrintWriter out=response.getWriter();
out.print(“输入的账号为:”+id+”密码为”+pwd);

doGet(request, response);
}

}

register sevlet

package login;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* Servlet implementation class registerServlet
*/
public class registerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public registerServlet() {
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
response.getWriter().append(“Served at: “).append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//乱码
response.setContentType(“text/html;charset=utf-8”);
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
int id = Integer.parseInt(request.getParameter(“id”));
String pwd=request.getParameter(“pwd”);
String pwd2=request.getParameter(“pwd2”);
PrintWriter out=response.getWriter();
out.print(“输入的账号为:”+id+”密码为”+pwd);

doGet(request, response);
}

}

 

usedata

package model;

public class usedata {
private Integer id;
private String Name;
private String password;

public usedata(Integer id, String name, String password) {
super();
this.id = id;
Name = name;
this.password = password;
}
public usedata(String name, String password) {
super();
Name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

 

test

import java.sql.SQLException;
import java.util.ArrayList;

import db.Database;
import model.usedata;

public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Database database = new Database();
database.showConnection();
database.insert(“asff”, “saffea”);

ArrayList<usedata> usedata = database.getAlluser();
System.out.println(“总表里面共有”+usedata.size());
for(usedata each:usedata) {
System.out.println(each.getId()+”—-“+each.getName()+”—–“+each.getPassword());
}
usedata usedata1 =database.getUser(1);
System.out.println(usedata1.getId()+usedata1.getName());
database.close();

}

}

 

 

html:

<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Insert title here</title>
</head>
<body>
<a herf=”http://localhost:8080/web001/register”>register</a>
<form action=”http://localhost:8080/web001/login” method =”post”>
账号:<input type = “text” name =”id”>
<br>
密码:<input type = “password” name =”pwd”>
<br>
<input type=”submit” value=”登录” type=”reset” value=”重新输入”>
</form>
</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>register</title>
</head>
<body>
<form action=”http://localhost:8080/web001/register” method =”post”>
账号:<input type = “text” name =”id”>
<br>
密码:<input type = “password” name =”pwd”>
<br>
确认密码:<input type = “password” name =”pwd2″>
<br>
<input type=”submit” value=”注册” type=”reset” value=”重新输入”>

</form>
</body>
</html>

 

原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/tech/database/273472.html

(0)
上一篇 2022年7月10日 22:54
下一篇 2022年7月10日 22:55

相关推荐

发表回复

登录后才能评论