前言
经过前面的环境部署,今天我们终于可以来连接数据库了。参考我之前的文章。这篇文章把前面两章连接起来,做一个小总结,并做一个可以保存用户登录数据的网站,代码主要解释对数据库的部分。
先前的开发环境安装:
centos7从零搭建java web服务器
MySQL从零安装配置
一,建立工程
1,在eclipse新建动态web工程,具体过程已经有很多人写过了,我就简单说明一下。
打开eclipse->file->new->dynamic web subject
tomcat版本选你安装的版本,方便以后调试,web module 版本我用2.5,跟3.0差不多。3.0少了web.xml的配置文件等。一路next下去就行了
二,导入jar包
连接MySQL需要导入MySQL相关的jar包,另外我还加入log包,对于开发非常有帮助,可以在控制台打印数据,方便debug。jar包就放下图示目录,另外还需要在项目右击->properties->java built path->libraries里添加我们刚刚添加的jar包,这样jar包才算真正导入;
jar包下载连接:
链接: https://pan.baidu.com/s/143RT1qygtKIimCRmLdpAbw 提取码: vd22
点击add jars 选择刚刚加入的jar包添加进去就行了
*
三,编写DataBase类实现连接数据库与操作数据库
以下代码需要自己先了解SQL语句,MySQL基本命令。代码实现了连接数据库,对数据库的某表格进行添加,读取,更新操作。对应的应用是添加用户,读取用户登录信息,更新密码。
注意数据保存到MySQL数据库出现中文乱码的问题,要在前端,后台,连接串,MySQL都设置相同的编码方式,比如我选择的是utf8
在MySQL命令中输入show variables like “%char%”;查看当前的编码,如图,都是utf8就没问题,需要修改的话可以查看修改办法
DataBase.java代码,主要实现对数据库连接和增改查操作
package myapp;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class DataBase {
private static String driver="com.mysql.jdbc.Driver";//MySQL驱动
private static String user = "prx";// MySQL的用户名和密码
private static String password = "5812870-Prx";
private static String ip_adress = "192.168.x.x";//主机ip地址
/*
* 说明:与数据库建立连接
* 参数:数据库名称
*/
private static Connection getConn(String DatabaseName) throws SQLException
{
//设置MySQL的连接串编码为UTF-8,避免中文乱码问题
String url="jdbc:mysql://localhost:3306/"+DatabaseName+"?characterEncoding=UTF-8";
System.out.println("the url is :"+url);
Connection connection=null;
try {
Class.forName(driver);//加载MySQL驱动程序
//连接数据库 驱动+ip地址+端口号+用户名+密码,端口号默认是3306
connection=(Connection) DriverManager.getConnection("jdbc:mysql://" + ip_adress + ":3306/" + DatabaseName,
user, password);
connection.setEncoding("UTF-8");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回connection对象
return connection;
}
/*
* 添加新用户
* String name,String sex,string age,String id,String password,String emailadress
*/
public static void adduser(String name,String sex,String age,String id,String password,String emailadress) throws SQLException
{
String Database="myapp";
String sql="INSERT INTO myapp_user (name,sex,age,id,password,emailadress) VALUES (?,?,?,?,?,?)";
Connection connection=getConn(Database);
if (connection!=null){
try {
PreparedStatement ps=connection.prepareStatement(sql);
if (ps!=null){
ps.setString(1,name);
ps.setString(2,sex);
ps.setString(3,age);
ps.setString(4,id);
ps.setString(5,password);
ps.setString(6,emailadress);
//执行语句,注意!!!如果你的SQL 语句是诸如update,insert的更新语句,应该用statement的execute()方法
// select用的是statement的executeQuery()
ps.execute();
connection.close();
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void updatepassword(String id,String newpassword) throws SQLException
{
String Database="myapp";
String sql="UPDATE myapp_user SET password=? where id=?";
Connection connection=getConn(Database);
if (connection!=null){
try {
PreparedStatement ps=connection.prepareStatement(sql);
if (ps!=null){
ps.setString(1,newpassword);
ps.setString(2,id);
//执行语句,注意!!!如果你的SQL 语句是诸如update,insert的更新语句,应该用statement的execute()方法
// select用的是statement的executeQuery()
ps.execute();
connection.close();
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
* 读取指定的值
* 输入id 键
*/
public static String sele_userinfo(String id,String key) throws SQLException {
String value = null;
String Database="myapp";
String sql="SELECT "+key+" FROM myapp_user WHERE id=?";
Connection connection=getConn(Database);
if (connection!=null){
try {
PreparedStatement ps=connection.prepareStatement(sql);
if (ps!=null){
ps.setString(1,id);
ResultSet rs=ps.executeQuery();
if(rs!=null) {
int count =rs.getMetaData().getColumnCount();
while (rs.next()){
for (int i=1 ;i<=count;i++){
String field=rs.getMetaData().getColumnName(i);
value=rs.getString(field);
System.out.println("the database value is:"+value);
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
/*
* 检查id'是否存在
* 传入id
*/
public static int hasId(String id) throws SQLException {
String Database="myapp";
String value;
String sql="select id from myapp_user";
System.out.println("the input id is :"+id);
Connection connection=getConn(Database);
if (connection!=null){
try {
PreparedStatement ps=connection.prepareStatement(sql);
if (ps!=null){
ResultSet rs=ps.executeQuery();
if(rs!=null) {
int count =rs.getMetaData().getColumnCount();
while (rs.next()){
for (int i=1 ;i<=count;i++){
String field=rs.getMetaData().getColumnName(i);
value=rs.getString(field);
System.out.println("the database id is:"+value);
if(value.equals(id))
{
return 1;
}
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}
}
四,效果图
登录页面
注册
在终端查看数据库,数据保存成功
登录成功!
整个工程文件下载:
链接: https://pan.baidu.com/s/1jq4LoetVdoksjjjGQeb4MA 提取码: 33xw 复制这段内容后打开百度网盘手机App,操作更方便哦
2020.3.16