创建对应的MySql数据库
创建Web工程,其中创建index.xml且设置为默认打开页面。
index.html如下:
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="getForm.jsp" method="POST">
姓名 <input type="text" name="Name"> <br />
电话号码: <input type="text" name="PhoneNun" /><br />
快递公司:
<select name="ExpressCmpy">
<option value="0">顺丰</option>
<option value="1">圆通</option>
<option value="2">申通</option>
<option value="3">韵达</option>
</select>
<br />
宿舍住址: <input type="text" name="Address" /><br />
物件大小:
<select name="Size">
<option value="0">大件</option>
<option value="1">中件</option>
<option value="2">小件</option>
</select><br />
拿快递时间: <select name="getTime">
<option value="0">11:30~13:30</option>
<option value="1">17:30~19:30</option>
</select><br />
备注:<input type="text" name =remark /><br />
<br />
<input align="center" type="submit" value="Submit" />
</form>
</center>
</body>
</html>
设置web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file-list />
</pre><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><!--在getForm.jsp中加入这段代码可以设置接受和发送的编码模式-->
<span style="font-family: Arial, Helvetica, sans-serif;"><%request.setCharacterEncoding("gb2312");%> </span>
<pre name="code" class="html"><%response.setCharacterEncoding("gb2312");%>
getUserInfo.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
import="com.bnuz66.*"%>
<%
request.setCharacterEncoding("gb2312");
%>
<%
response.setCharacterEncoding("gb2312");
%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<body>
<center>
<jsp:useBean id="use" scope="page" class="com.bnuz66.User">
</jsp:useBean>
<jsp:setProperty property="*" name="use" />
<%
final int ONROAD = 0;
final int FINISH = 1;
final int CANCEL = 2;//charset=ISO-8859-1
use.setName(request.getParameter("Name"));
use.setPhoneNum(request.getParameter("PhoneNum"));
use.setExpressCmpy(request.getParameter("ExpressCmpy"));
use.setAddress(request.getParameter("Address"));
switch (Integer.parseInt((request.getParameter("Size")))) {
case 0:
use.setSize(0);
use.setPrice(4);
case 1:
use.setSize(1);
use.setPrice(7);
case 2:
use.setSize(2);
use.setPrice(10);
}
use.setGetTime(Integer.parseInt((request.getParameter("getTime"))));
use.setRemarks(request.getParameter("remarks"));
use.setStatus(ONROAD);
out.println(use.getName() + "\t");
out.println(use.getPhoneNum() + "\t");
out.println(use.getExpressCmpy() + "\t");
out.println(use.getAddress() + "\t");
out.println(use.getSize() + "\t");
out.println(use.getGetTime() + "\t");
out.println(use.getPrice() + "\t");
out.println(use.getRemarks() + "\t");
out.println(use.getStatus() + "\t");
use.upLoad2Sql();
%>
</center>
</body>
</html>
这里是设置输入jsp的编码设置
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8"%>
pageEncoding需要是utf-8才能显示中文。
参考:http://xifeng91.iteye.com/blog/372878
User.java类
package com.bnuz66;
public class User {
final static int ONROAD = 0;
final static int FINISH = 1;
final static int CANCEL = 2;
private String name;
private String PhoneNum;
private String ExpressCmpy;
private String Address;
private int Size;
private int getTime;
private int price;
private String remarks;
private int status;
public void upLoad2Sql(){
new UpLoadService(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return PhoneNum;
}
public void setPhoneNum(String PhoneNum) {
this.PhoneNum = PhoneNum;
}
public String getExpressCmpy() {
return ExpressCmpy;
}
public void setExpressCmpy(String expressCmpy) {
ExpressCmpy = expressCmpy;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public int getSize() {
return Size;
}
public void setSize(int size) {
Size = size;
}
public int getGetTime() {
return getTime;
}
public void setGetTime(int getTime) {
this.getTime = getTime;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
UpLoadService.java 类
package com.bnuz66;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import com.sun.net.httpserver.Authenticator.Result;
import com.sun.org.apache.bcel.internal.generic.DMUL;
public class UpLoadService {
Connection conn = null;
Statement stmt = null;
Result rs = null;
String sql = null;
public UpLoadService(User user) {
// TODO Auto-generated constructor stub
try {
// String databaseName = "RbMwPrdlElXBMixvJNjX";
// String host = "sqld.duapp.com";
// String port = "4050";
// String username = "gIPwyeDhKVykpPTa4i2w83Q6";
// String password = "GfbVVaYhVpgwN3FPEVsxKj0XuRhfzW3C";
String databaseName = "CilentInfo";
String host = "localhost";
String port = "3306";
String username = "root";
String password = "awd4294967299#";
String driverName = "com.mysql.jdbc.Driver";
String dbUrl = "jdbc:mysql://";
String serverName = host + ":" + port + "/";
String connName = dbUrl + serverName + databaseName;
Class.forName(driverName);
conn = DriverManager.getConnection(connName, username, password);
stmt = conn.createStatement();
sql = InsertData(user);
stmt.execute(sql);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private String InsertData(User user) {
// TODO Auto-generated method stub
// String rs_sql = "insert into User(Name,PhoneNum,Address,ExpressCmpy,Size,getTime,price,remarks,status) ";
// rs_sql += "values(" + user.getName() + "," + user.getPhoneNum() + ","
// + user.getAddress() + "," + user.getExpressCmpy()
// + user.getSize() + "," + user.getGetTime() + ","
// + user.getPrice() + user.getRemarks() + "," + user.getStatus()
// + ")";
String rs_sql =
"insert into User(Name,PhoneNum,Address,ExpressCmpy,Size,getTime,price,remarks,status) ";
rs_sql += "values('" + user.getName() + "','" + user.getPhoneNum()
+ "','" + user.getAddress() + "','" + user.getExpressCmpy()
+ "','" + user.getSize() + "','" + user.getGetTime() + "','"
+ user.getPrice() + "','" + user.getRemarks() + "','"
+ user.getStatus() + "')";
return rs_sql;
}
}
插入数据库语言的格式:Insert into table_name (v_1,v_2,v_3), value(data1,data2,data3)
一般主键会增加Auto Increa的标记
#1062 – Duplicate entry '1′ for key ‘PRIMARY'
这个错误是由于插入时的主键已经存在相同值,只要主键是Auto Inc就不需要考虑在插入时编辑主键。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
这个错误是由于SQL语法错误,在语句中有关键字的变量引起的
或者!!!我就是我的错误!!!
如果输入的是列变量,一定要看清楚 ‘ 这个单分号!!!很容易多或者少!!一定要检查!
参考:http://www.inmotionhosting.com/support/website/database-troubleshooting/error-1064