原生Java操作mysql数据库过程解析

网友投稿 258 2023-05-28

原生Java操作mysql数据库过程解析

这篇文章主要介绍了原生java操作mysql数据库过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.引入http://数据库驱动的jar包 以通过maven引入mysql driver为例

1.1 到http://mvnrepository.com 搜索 mysql

1.2 复制所需maven配置文件到工程的 pom.xml

mysql

mysql-connector-java

6.0.6

2.创建数据库连接类DBUtil.java用以连接与关闭数据库

//文件名:DBUtil.java

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class DBUtil {

static String user = "root";

static String password = "root";

static String url = "jdbc:mysql://localhost【数据库地址】:3306【端口】/【数据库名称】?serverTimezone=UTC";

static {

try {

Class.forName("com.mysql.cj.jdbc.Driver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

public static Connection getConnection() {

Connection conn = null;

try {

conn = DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public static void closeJDBC(ResultSet rs, Statement stmt, Connection conn) {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (stmt != null) {

try {

stmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

3.在java代码中对表进行操作

3.1 查,删,改类似

//查找table表重的 id和name

String sql = "select id,name from table";

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = null;

try {

conn.setAutoCommit(false);

pstmt = conn.prepareStatement(sql);

ResultSet rs = pstmt.executeQuery();

json = ResultSetToJson.ResultSetToJsonArray(rs);

}catch (SQLException e){

try {

conn.rollback();

}catch (SQLException e1){

e1.printStackTrace();

}

}finally {

DBUtil.closeJDBC(null, pstmt, conn);

}

return json;

3.1 增

int basicinfoID = 0;

String sql = "INSERT INTO tb_resume_basicinfo("

    + "basicinfo_id, realname, gender, birthday, current_loc, "

    + "resident_loc, telephone, email, job_intension, job_experience, head_shot,applicant_id) "

    +"VALUES(SEQ_ITOFFER_RESUMEBASICINFO.NEXTVAL,?,?,?,?,?,?,?,?,?,?,?)";

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = null;

try {

// 关闭自动提交

conn.setAutoCommit(false);

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, basicinfo.getRealName());

pstmt.setString(2, basicinfo.getGender());

pstmt.setTimestamp(3, basicinfo.getBirthday() == null ? null

: new Timestamp(basicinfo.getBirthday().getTime()));

pstmt.setString(4, basicinfo.getCurrentLoc());

pstmt.setString(5, basicinfo.getResidentLoc());

pstmt.setString(6, basicinfo.getTelephone());

pstmt.setString(7, basicinfo.getEmail());

pstmt.setString(8, basicinfo.getJobIntension());

pstmt.setString(9, basicinfo.getJobExperience());

pstmt.setString(10, basicinfo.getHeadShot());

pstmt.setInt(11, applicantID);

pstmt.executeUpdate();

} catch (SQLException e) {

try {

// 事务回滚

conn.rollback();

} catch (SQLException e1) {

e1.printStackTrace();

}

e.printStackTrace();

} finally {

DBUtil.closeJDBC(null, pstmt, conn);

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java通过socket客户端保持连接服务端实现代码
下一篇:基于java使用钉钉机器人向钉钉群推送消息
相关文章

 发表评论

暂时没有评论,来抢沙发吧~