Java链接MySQL数据库 博客分类: JavaMySQL mysqljavadatabase数据库
程序员文章站
2024-03-05 21:56:37
...
Java链接MySQL数据库创建方式
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class MySQLDAO { private String ip = null; private String port = null; private String database = null; private String user = null; private String password = null; private Connection con = null; /** * * @param ip * @param port * @param database * @param user * @param password */ public MySQLDAO(String ip, String port, String database, String user, String password) { this.ip = ip; this.port = port; this.database = database; this.user = user; this.password = password; } /** * * @return Connection */ public Connection getMySQLConnection() { int time = 0; String url = "jdbc:mysql://" + ip + ":" + port + "/" + database + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull"; try { for (; (++time < 3) && (con == null || con.isClosed());) { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, user, password); } } catch (Exception e) { System.err.println("Cannot get MySQL database connection."); e.printStackTrace(); } return con; } /** * * @param con * Connection * @return */ public boolean closeMySQLResource(Connection con) { return closeMySQLResource(con, null, null); } /** * * @param ps * Statement * @return */ public boolean closeMySQLResource(Statement ps) { return closeMySQLResource(null, ps, null); } /** * * @param rs * ResultSet * @return */ public boolean closeMySQLResource(ResultSet rs) { return closeMySQLResource(null, null, rs); } /** * * @param con * Connection * @param ps * Statement * @return */ public boolean closeMySQLResource(Connection con, Statement ps) { return closeMySQLResource(con, ps, null); } /** * * @param ps * Statement * @param rs * ResultSet * @return */ public boolean closeMySQLResource(Statement ps, ResultSet rs) { return closeMySQLResource(null, ps, rs); } /** * * @param con * Connection * @param rs * ResultSet * @return */ public boolean closeMySQLResource(Connection con, ResultSet rs) { return closeMySQLResource(con, null, rs); } /** * * @param con * Connection * @param ps * Statement * @param rs * ResultSet * @return */ public boolean closeMySQLResource(Connection con, Statement ps, ResultSet rs) { try { if (con != null) con.close(); if (ps != null) ps.close(); if (rs != null) rs.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
推荐阅读
-
Linux下C++连接操作MySQL数据库环境搭建 博客分类: c/c++LinuxMySQL c/c++LinuxMySQLgccboost
-
Java链接MySQL数据库 博客分类: JavaMySQL mysqljavadatabase数据库
-
Java链接Oracle数据库 博客分类: JavaOracle oraclejavajdbc
-
MySQL命令大全(转载) 博客分类: MySQL数据库 mysqlcommand
-
Linux搭建Mysql数据库开发环境 博客分类: SQL技术栈 Linuxmysql数据库优化
-
浅谈MySQL外键 博客分类: 数据库 MySQL外键
-
mysql 外键设置 博客分类: 数据库 mysql外键数据库
-
为什么要用非关系型数据库 博客分类: mysql NoSQLSQLSQL ServerSNSHBase
-
使用mysql federated引擎构建MySQL分布式数据库访问层 博客分类: Mysql
-
SSH+MYSQL 数据库连接失败问题 博客分类: 数据库 mysqlhibernatessh