二月二十一号
今天练习时间大约一小时左右。解决了mysql数据库不能输入中文的编码问题,解决办法是打开my.ini文件,在在[client]下加default-character-set = utf8、在在[mysqld]下加character-set-server = utf8,然后重新创建表就可以解决无法输入中文的问题。另外完成了昨天的小测试,连接数据库并完成数据库的增和查功能,并了解了一下一个完整增删改查项目的大致流程,每个文件之间是怎么调用的,是如何联系到一起的。
package bao;
public class xinxi {
String id,name,number,classs;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getClasss() {
return classs;
}
public void setClasss(String classs) {
this.classs = classs;
}
}
**************************************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在此处插入标题</title>
</head>
<body>
<p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
<form action="addxinxi.jsp" method="post" onsubmit="return check(this);">
信息登记页面
<br>id<input type="text" name="id" size="40"/> </br>
<br>用户名 <input type="text" name="name" size="40"/> </br>
<br>手机号码<input type="text" name="number"size="40"/></br>
<br>班级<input type="text" name="classs" size="40"/><br>
<br><input type="submit" value="提交" />
<input type="reset" value="重置" /> <br>
</p>
</body>
</html>
***********************************************
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.PreparedStatement" %>
<html>
<head>
<title>添加结果</title>
<style type="text/css">
body{
background: #d7c7e9;
align-content: center;
text-align: center;
}
</style>
</head>
<body>
<%request.setCharacterEncoding("UTF-8");%>
<jsp:useBean id="xinxi" class="bao.xinxi"></jsp:useBean>
<jsp:setProperty name="xinxi" property="*"/>
<%
Connection conn =null;
try{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
out.print("驱动器加载失败");
}
String url = "jdbc:mysql://localhost:3306/ceshi?serverTimezone=GMT%2B8";
String username = "root";
String password = "NN06280055";
try{
conn = DriverManager.getConnection(url,username,password);
}catch (SQLException e){
out.println(e.getMessage());
out.println("数据库连接失败");
}
if(conn != null){
String sql = "insert into xx(id,name,number,classs)values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,xinxi.getId());
ps.setString(2,xinxi.getName());
ps.setString(3,xinxi.getNumber());
ps.setString(4,xinxi.getClasss());
int row = ps.executeUpdate();
if(row>0){
out.println("成功添加了" + row + "条数据!");
}
}
}catch (Exception e){
e.printStackTrace();
out.print("图书信息添加失败!");
}
%>
<br>
<a href="add.jsp">返回</a>
</body>
</html>