视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
MySQL的简单操作方法:Statement、PreparedStatement_MySQL
2020-11-09 19:48:47 责编:小采
文档


(1)连接mysql的工具类:DBUtil.java

package com.xuliugen.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
 public static Connection getConn() {
 Connection conn = null;
 try {
 Class.forName("com.mysql.jdbc.Driver");
 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/你的数据库名称", "你的账号", "你的密码");
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return conn;
 }

 public static PreparedStatement prepareStmt(Connection conn, String sql) {
 PreparedStatement pstmt = null;
 try {
 pstmt = conn.prepareStatement(sql);
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return pstmt;
 }

 public static ResultSet executeQuery(Statement stmt, String sql) {
 ResultSet rs = null;
 try {
 rs = stmt.executeQuery(sql);
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return rs;
 }


 public static void close(Connection conn, Statement stmt,
 PreparedStatement preStatement, ResultSet rs) {
 if (conn != null) {
 try {
 conn.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 conn = null;
 }
 if (stmt != null) {
 try {
 stmt.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 stmt = null;
 }
 if (preStatement != null) {
 try {
 preStatement.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 preStatement = null;
 }

 if (rs != null) {
 try {
 rs.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 rs = null;
 }
 }
}

(2)使用preparedStatement插入数据的数据库:

public boolean saveComment(Comment comment) {

 Connection connection = DBUtil.getConn();
 String sql = "insert into comment values (null,?,?,?,?)";
 PreparedStatement preparedStatement = null;
 boolean flag = false;
 try {
 preparedStatement = connection.prepareStatement(sql);
 preparedStatement.setString(1, comment.getCommenttext() + "");
 preparedStatement.setString(2, comment.getCommenttime() + "");
 preparedStatement.setString(3, comment.getUserid() + "");
 preparedStatement.setString(4, comment.getArticleid() + "");
 int isOk = preparedStatement.executeUpdate();
 if (isOk > 0) {
 return !flag;
 } else {
 return flag;
 }
 } catch (SQLException e) {
 e.printStackTrace();
 }
 DBUtil.close(connection, null, preparedStatement, null);
 return flag;
 }

(3)使用preparedStatement选择数据,读取数据:

public List getCommentDetail(int userid, int articleid) {
 Connection connection = DBUtil.getConn();
 String sql = "select * from comment c where c.userid=? and c.articleid=?";// 编写sql语句,第一个字段不需要插入,是自动增加的
 PreparedStatement preparedStatement = null;
 List commentList = new ArrayList();
 try {
 preparedStatement = connection.prepareStatement(sql);
 preparedStatement.setInt(1, userid);
 preparedStatement.setInt(2, articleid);
 //这里的产讯不需要传入sql语句
 ResultSet rs = preparedStatement.executeQuery();

 // 判断是否为空
 if (rs.next()) {
 while (rs.next()) {// 将信息迭代到list中
 Comment comment = new Comment();
 comment.setCommentid(rs.getInt("commentid"));
 comment.setCommenttext(rs.getString("commenttext"));
 comment.setCommenttime(rs.getString("commenttime"));
 comment.setUserid(rs.getInt("userid"));
 comment.setArticleid(rs.getInt("articleid"));

 commentList.add(comment);
 }
 } else {
 return null;
 }

 } catch (SQLException e) {
 e.printStackTrace();
 }
 DBUtil.close(connection, null, preparedStatement, null);
 return commentList;
 }

(4)使用Statement操作数据库:

public List getArticleMessage() {
 Connection connection = DBUtil.getConn();
 String sql = "select * from article";// 编写sql语句,第一个字段不需要插入,是自动增加的
 Statement statement = null;
 List articleList = new ArrayList();
 try {
 statement = connection.createStatement();
 ResultSet rs = statement.executeQuery(sql);
 //判断是否为空
 if (rs.next()) {
 while (rs.next()) {//将信息迭代到list中
 Article article = new Article();
 article.setTitle(rs.getString("title"));
 article.setContext(rs.getString("context"));
 article.setArticleTime(rs.getString("articletime"));
 article.setUserid(rs.getInt("userid"));
 article.setArticleid(rs.getInt("articleid"));
 articleList.add(article);
 }
 } else {
 return null;
 }

 } catch (SQLException e) {
 e.printStackTrace();
 }
 DBUtil.close(connection, statement, null, null);
 return articleList;
 }

下载本文
显示全文
专题