mysql下载jar包初学者Java(⼗四)IdeaServlet与Mysql进⾏简单的增删改查⼀:前提准备⼯作
1:需要安装 Tomcat 运⾏环境 加载jar包
2:单元测试环境可以安装,JUnit4 Jar包
3:加载 JDK
4:加载数据库jar包
5:安装Servlet-api 的jar包
6:安装 jsp-api 的jar包
项⽬整体框架,开发只需要添加项⽬的两块:后端:src 前端:web
⼆:底层数据库连接:
1:代码:⾸先需要建应该properties后缀名的⽂件。
# sql.jdbc.Driver
sql.jdbc.Driver
url=jdbc:mysql://10.3.90.84:3306/crs_miniservice?serverTimezone=UTC&characterEncoding=utf-8 username=crs_mysql_test
password=crs123456
2:新建⼀个dbhelp的java类,⾥⾯去上⾯的配置信息。
DBHelper 类
package cn.servlet.dbhelp;
import cn.servlet.utils.ConfigManager;
import java.sql.*;
public class DBHelper {
//region attributes
Connection conn = null;
PreparedStatement preparedStatement;
ResultSet resultSet = null;
//endregion
//region method
//数据库连接
public boolean getConnection(){
try {
Class.Instance().getPropertyValue("driver"));
conn = Instance().getPropertyValue("url"), Instance().getPropertyValue("username"),
return true;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
//查询,返回结果集
public ResultSet getResultSet(String sql,Object[] parms) {
if (getConnection()) {
if (getConnection()) {
try {
preparedStatement = conn.prepareStatement(sql);
for (int i = 0; i < parms.length; i++) {
preparedStatement.setObject(i+1,parms[i]);
}
resultSet = uteQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
return resultSet;
}
/
/删除,更新,添加,返回受影响的⾏数
public int getExecuteUpdateSQL(String sql,Object[] parms) throws SQLException, ClassNotFoundException { int rows=0;
if (getConnection()) {
try {
preparedStatement = conn.prepareStatement(sql);
for (int i = 0; i < parms.length; i++) {
preparedStatement.setObject(i+1,parms[i]);
}
} catch (SQLException e) {
e.printStackTrace();
}
rows = uteUpdate();
}
return rows;
}
//endregion
}
配置⽂件类:
ConfigManager类的代码如下:根据下⾯⽅法获取properties⽂件的配置信息
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//数据库配置⼯具类
public class ConfigManager {
//region attributes
private static ConfigManager configManager;
private Properties properties;
private InputStream resourceAsStream;
//endregion
//region methods
public ConfigManager() {
properties = new Properties();
String filePath="./resources/database.properties";
resourceAsStream = ClassLoader().getResourceAsStream(filePath); try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建ConfigManager实例
public static ConfigManager getInstance(){
if (configManager ==null) {
configManager = new ConfigManager();
}
return configManager;
}
//通过key获取属性值
public String getPropertyValue(String key){
Property(key);
}
//endregion
}
三:实体类:UserInfo
public class UserInfo {
//region attributes
public int id;
private String userName;
private int sex;
private int age;
private String job;
private String description;
//endregion
//region method
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) { this.userName = userName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getDescription() {
return description;
}
public void setDescription(String description) { this.description = description;
}
/
/endregion
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论