这期内容当中小编将会给大家带来有关Java怎么操作MySQL数据库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联建站是一家专注于成都网站建设、做网站与策划设计,南开网站建设哪家好?创新互联建站做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:南开等地区。南开做网站价格咨询:18982081108
1.引入数据库驱动的jar包 以通过maven引入mysql driver为例
1.2 复制所需maven配置文件到工程的 pom.xml
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency>
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); }
上述就是小编为大家分享的Java怎么操作mysql数据库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网站标题:Java怎么操作mysql数据库
路径分享:https://www.cdcxhl.com/article18/pdcedp.html
成都网站建设公司_创新互联,为您提供外贸网站建设、响应式网站、移动网站建设、虚拟主机、网站内链、定制网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联