网页 servlet My sql数据库 三层实现

这个是基于之前学的 HTML网页,servlet的reques(请求)response(响应),数据库编程一起组合起来实现的

注意:

首先要解决乱码问题,前面有介绍,还有俩个常见报错

java.lang.NullPointerException    空指针异常   

java.lang.ClassNotFoundException   类无法加载异常 

解决方法:在WebRoot下的Web-INF下的LIB中导入mysql的jar包就可以了

一共三个类和一个HTML文件

 UserInfo     

package cn.qyc.dao;

public class UserInfo {
	private String username;
	private String password;
	private String sex;
	private String hobby;
	private String address;
	
	private String meno;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	
	public String getMeno() {
		return meno;
	}
	public void setMeno(String meno) {
		this.meno = meno;
	}
	public UserInfo(String username, String password, String sex, String hobby, String address,String meno) {
		super();
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.hobby = hobby;
		this.address = address;
		
		this.meno = meno;
	}
	public UserInfo() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

JDBCConnection     连接数据库类

 

package cn.qyc.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCConnection {
	static Connection conn = null;
	static String URL = "jdbc:mysql://localhost:3306/mysql";
	//加载驱动程序  建立数据库连接
//	@Test
	public static Connection getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Driver成功");
			conn = DriverManager.getConnection(URL,"root","123456");
			System.out.println(conn);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return conn;

	}
	//关闭所有链接
	public static void close() throws SQLException{
		conn.close();
	}
}

InforService     操作数据库类(实现了增)

package cn.qyc.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.xml.soap.Text;

import org.junit.Test;

import cn.qyc.connection.JDBCConnection;
import cn.qyc.dao.UserInfo;

public class  InforService {
 
	Connection conn = null;
	Statement stmt = null;
	public  void insert(UserInfo pe){
			System.out.println("调用insert()函数  ");
			String sqlString = "insert into userxx (username,password,sex,habby,address,meno) values ('"+pe.getUsername()+"','"+pe.getPassword()+"','"+pe.getSex()+"','"+pe.getHobby()+"','"+pe.getAddress()+"','"+pe.getMeno()+"')";
			conn = JDBCConnection.getConnection();
			try {
				System.out.println(conn);
				System.out.println("新对象");
				stmt = conn.createStatement();
				System.out.println("执行stmt");
				int row = stmt.executeUpdate(sqlString);
				if(row==1)
				System.out.println("添加成功!");
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
        public int dl(JavaB people){
		conn = JDBCConnection.getConnection();
		String sql = "select* from login where number='"+people.getNumber()+"'";
		System.out.println(people.getNumber());
		try {
			statement = conn.createStatement();
			ResultSet set = statement.executeQuery(sql);
			set.next();
//			System.out.println(set.getString(3)+set.getString(4));
			if(set.getString(3).compareTo(people.getName())==0&&set.getString(4).compareTo(people.getPassword())==0){
				conn.close();
				statement.close();
				return 1;
			}
			else{
				conn.close();
				statement.close();
				System.out.println("登陆失败");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return 0;
		
	}
}

LoginServlet  (Servlet关键,在这里运行)

package com.servletDemo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.qyc.dao.UserInfo;
import cn.qyc.service.InforService;

public class LoginServlet extends HttpServlet {

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserInfo people = new UserInfo();
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=gb2312");
		people.setUsername(request.getParameter("username"));
		people.setPassword(request.getParameter("password"));
		people.setSex(request.getParameter("sex"));
		String habbString[] = request.getParameterValues("hobby");
		String aihaoString="";
		if(habbString!=null)
		for(int i=0;i<habbString.length;i++)
			aihaoString += habbString[i]+",";
		
		people.setHobby(aihaoString);
		people.setAddress(request.getParameter("address")+request.getParameter("shi")+request.getParameter("shi"));
		
		people.setMeno(request.getParameter("meno"));
		InforService caozuo = new InforService();
		System.out.println("建立InforService对象");
		caozuo.insert(people);
	}





	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

 MyJsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <form action="LoginServlet" method="post">
			<table border="1" cellspacing="" cellpadding="">
				<tr><td align="center" colspan="2">用户注册页面</td></tr>
				<tr><td>用户名:</td><td><input type="text" name="username" size="29"></td></tr>
				<tr><td>密码:</td><td><input type="password" name="password" size="30"></td></tr>
				<tr><td align="center" colspan="2"><input type="button" value="确定" size="10"></td></tr>
				<tr><td>性别:</td><td><input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="nv">女</td></tr>
				<tr><td>爱好:</td><td><input type="checkbox" name="hobby" value="玩">玩游戏 
				<input type="checkbox" name="hobby" value="吃">吃东西 
				<input type="checkbox" name="hobby" value="睡">睡觉</td></tr>
				<tr><td>家庭住址:</td><td><select name="address">
				<option value="黑龙江">黑龙江</option>
				<option value="四川">北京</option>
				<option value="山西">山西</option>
				</select>
				<select name="shi">
				<option value="朔州市">朔州市</option>
				<option value="哈尔滨">哈尔滨</option>
				<option value="成都市">成都</option>
				</select>
				<select name="qu">
				<option value="朔城区">朔城区</option>
				<option value="宾西镇">宾西镇</option>
				<option value="村里">村里</option>
				</select>
				</td></tr>
				<tr><td colspan="2">上传文件:<input type="file"></td></tr>
				<tr><td align="center">其他:</td><td><textarea name="meno" rows="10" cols="30">
				</textarea></td></tr>
				<tr><td colspan="2" align="center"><input type="submit" value="提交"></td></tr>
				
			</table>
			</form>
  </body>
</html>

有时间再实现增删改查(ps很简单,难点主要是那俩个报错

          

          天净沙~秋 (朱庭玉)
            
            庭前落尽梧桐,
            水边开彻芙蓉。
            解与诗人意同。
            辞柯霜叶,
            飞来与我题红。

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值