java连接mysql增_java连接MySQL增加信息

package com.dao;//导入信息

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import com.entry.user;

import com.util.DBUtil;

public class userDao {

public boolean add(user user) {

String sql = "insert into user2(id,password,sex,name,no,mail,xy,x,bj,nf,syd,bz) "

+ "values('" + user.getId() + "','" + user.getPassword() + "','" + user.getSex() + "','" + user.getName() + "','" + user.getNo() + "','" + user.getMail() + "','" + user.getXy() + "','" + user.getX() + "','" + user.getBj() + "','" + user.getNf() + "','" + user.getSyd() + "','" + user.getBz() + "')";

Connection conn = DBUtil.getConn();

Statement state = null;

boolean f = false;

int a = 0;

try {

state = conn.createStatement();

state.executeUpdate(sql);

} catch (Exception e) {

e.printStackTrace();

} finally {

DBUtil.close(state, conn);

}

if (a > 0) {

f = true;

}

return f;

}

public boolean id(String id) {

boolean f = false;

String sql = "select id from user2 where name = '" + id + "'";

Connection conn = DBUtil.getConn();

Statement state = null;

ResultSet rs = null;

try {

state = conn.createStatement();

rs = state.executeQuery(sql);

while (rs.next()) {

f = true;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

DBUtil.close(rs, state, conn);

}

return f;

}

}

输入要的信息变量

package com.entry;

public class user {

private String id;

private String name;

private String password;

private String no;

private String mail;

private String xy;

private String sex;

private String x;

private String bj;

private String nf;

private String syd;

private String bz;

public String getId() {

return id;

}

public String getX() {

return x;

}

public void setX(String x) {

this.x = x;

}

public String getBj() {

return bj;

}

public void setBj(String bj) {

this.bj = bj;

}

public String getNf() {

return nf;

}

public void setNf(String nf) {

this.nf = nf;

}

public String getSyd() {

return syd;

}

public void setSyd(String syd) {

this.syd = syd;

}

public String getBz() {

return bz;

}

public void setBz(String bz) {

this.bz = bz;

}

public void setId(String id) {

this.id = id;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getNo() {

return no;

}

public void setNo(String no) {

this.no = no;

}

public String getMail() {

return mail;

}

public void setMail(String mail) {

this.mail = mail;

}

public user(){}

public user(String name, String password) {

this.name = name;

this.password = password;

};

public user(String id, String name, String password, String no, String mail, String xy, String sex, String x, String bj,

String nf, String syd, String bz) {

super();

this.id = id;

this.name = name;

this.password = password;

this.no = no;

this.mail = mail;

this.xy = xy;

this.sex = sex;

this.x = x;

this.bj = bj;

this.nf = nf;

this.syd = syd;

this.bz = bz;

}

public String getXy() {

return xy;

}

public void setXy(String xy) {

this.xy = xy;

}

}

检测是否导入

package com.service;

import com.dao.userDao;

import com.entry.user;

public class userservice {

userDao cDao = new userDao();

public boolean add(user user) {

boolean f = false;

if(!cDao.id(user.getId())) {

cDao.add(user);

f = true;

}

return f;

}

}

package com.servlet;

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;

import com.entry.user;

import com.service.userservice;

@WebServlet("/AddServlet")

public class AddServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

userservice service = new userservice();

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

String method = req.getParameter("method");

if("add".equals(method))

doGet(req,resp);

}

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

request.setCharacterEncoding("utf-8");

String id = request.getParameter("id");

String password = request.getParameter("password");

String sex = request.getParameter("sex");

String name = request.getParameter("name");

String no = request.getParameter("no");

String mail = request.getParameter("mail");

String xy = request.getParameter("xy");

String x = request.getParameter("x");

String bj = request.getParameter("bj");

String nf = request.getParameter("nf");

String syd = request.getParameter("syd");

String bz = request.getParameter("bz");

user user= new user(id, name, password, no, mail, xy, sex, x, bj,

nf, syd, bz);

System.out.println(id);

if(service.add(user)) {

request.setAttribute("message");

request.getRequestDispatcher("NewFile.jsp").forward(request,response);

} else {

request.setAttribute("message");

request.getRequestDispatcher("NewFile.jsp").forward(request,response);

}

}

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

doGet(request, response);

}

}

连接数据库

package com.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class DBUtil {

public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8";

public static String db_user = "root";

public static String db_pass = "root";

public static Connection getConn () {

Connection conn = null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(db_url, db_user, db_pass);

} catch (Exception e) {

e.printStackTrace();

}

return conn;

}

public static void close (Statement state, Connection conn) {

if (state != null) {

try {

state.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void close (ResultSet rs, Statement state, Connection conn) {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (state != null) {

try {

state.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) throws SQLException {

Connection conn = getConn();

PreparedStatement pstmt = null;

ResultSet rs = null;

String sql ="select * from user";

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

if(rs.next()){

System.out.println("添加成功");

}else{

System.out.println("添加失败");

}

}

}

做注册界面

pageEncoding="UTF-8"%>

Insert title here注册页面

Object message = request.getAttribute("message");

if(message!=null && !"".equals(message)){

%>

alert("");

注册
登录账号
登录密码
性别:

姓名
学号
电子邮件
所在学院
所在系
所在班级
入学年份(届):

2017

2018

生源地:
备注:
保   存         

重   置

/*

表单验证

*/

var flag = false; // flag 如果为true(即用户名合法)就允许表单提交, 如果为false(即用户名不合法)阻止提交

function focus_pass()

{

var nameObj = document.getElementById("result1");

nameObj.innerHTML = "由八位以上字符和数字组成";

nameObj.style.color="#999";

}

function blur_pass()

{

var nameObj = document.getElementById("result1");

// 判断用户名是否合法

var str2 = check_user_pass(document.form1.password.value);

nameObj.style.color="red";

if ("密码合法" == str2)

{

flag = true;

nameObj.innerHTML = str2;

}

else

{

nameObj.innerHTML = str2;

}

}

function check_user_pass(str)

{ var str2 = "密码合法";

if ("" == str)

{

str2 = "密码为空";

return str2;

}

else if (str.length<8)

{

str2 = "密码应为8位组成";

return str2;

}

else if (!check_word(str))

{

str2 = "未含有英文字符";

return str2;

}

return str2;

}

下载GDBC复制到lib项目下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值