1.数据库准备
create database whoere; //创建数据库
use whoere;
create table email( //创建数据表,根据需要自行定义,比如基本的账号密码,我还设置了经纬度用于获取用户的位置
`id` int unsigned auto_increment,
`account` varchar(20) not null,
`password` varchar(20) not null,
`Latitude` double,
`Longitude` double,
`Intention` varchar(100),
primary key(`id`)
)engine=InnoDB default charset=utf8;
设置模拟数据(随意):
2.转战NetBeans(各种IDE都可以啦,比如Eclipse、Ij之类的)
安装过程我就不废话了,直接撸码!(需要用到的jar包也放在百度云了,记得导入库中哦)
首先是User类,都是基本的get和set函数。
package net.whoere;
/**
*
* @author SuSu
*/
public class User {
//账户(email)
private String account;
//密码
private String password;
//经度
private double Latitude;
//纬度
private double Longitude;
//意图
private String Intention;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getLatitude(){
return Latitude;
}
public void setLatitude(double Latitude){
this.Latitude = Latitude;
}
public double getLongitude(){
return Longitude;
}
public void setLongitude(double Longitude){
this.Longitude = Longitude;
}
public String getIntention(){
return Intention;
}
public void setIntention(String Intention){
this.Intention = Intention;
}
}
接着是DBManager类,实现数据库的连接与关闭。
package net.whoere;
/**
*
* @author SuSu
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class DBManager extends HttpServlet {
ServletConfig config; //定义一个ServletConfig对象
private static String username; //定义数据库用户名
private static String password; //定义数据库连接密码
private static String url; //定义数据库连接URL
private static Connection connection; //定义连接
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config); //继承父类的init()方法
this.config = config; //获取配置信息
username = config.getInitParameter("DBUsername"); //获取数据库用户名
password = config.getInitParameter("DBPassword"); //获取数据库连接密码
url = config.getInitParameter("ConnectionURL"); //获取数据库连接URL
}
//连接数据库
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | SQLException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
}
return connection;
}
//关闭连接,ResultSet是返回数据库的查询内容。
public static void closeAll(Connection connection, Sta