Java链接mySQL数据库进行增删改查_MySQL
Java链接mySQL数据库代码
改和查对于增加是一样的
public class JDBCTest { /** * ResultSet封装了JDBC结果集,进行查询的结果 */ @Test public void testResultSet() { Connection connection = null; Statement statement = null; ResultSet rs = null; try { connection = JDBCTools.getConnection(); statement = (Statement) connection.createStatement(); String sql = "SELECT * FROM jdbc_1"; rs = statement.executeQuery(sql); while (rs.next()) { System.out.print(rs.getInt(1) + "/t"); System.out.print(rs.getString(2) + "/t"); System.out.print(rs.getString(3)); System.out.println(); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.release(rs, statement, connection); } } /** * 通用更新方法 * @throws SQLException */ public void updata(String sql) throws SQLException { Connection connection = null; Statement statement = null; try { connection = getConnection(); // 获取Statement 对象 statement = (Statement) connection.createStatement(); // 执行sql语句 statement.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { // 关闭statement对象 if (statement != null) { statement.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭数据库连接 if (connection != null) { connection.close(); } } } } /** * 1.通过jdbc向指定表中插入数据 * * @throws Exception * */ @Test public void testStatement() throws Exception { // 获取数据库链接 Connection connection = null; Statement statement = null; try { connection = getConnection(); // 要执行的sql语句 String sql = "INSERT INTO jdbc_1 (jname,addr) VALUE ('李力','浑江市')"; // 获取Statement 对象 statement = (Statement) connection.createStatement(); // 执行sql语句 statement.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { // 关闭statement对象 if (statement != null) { statement.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭数据库连接 if (connection != null) { connection.close(); } } } } /** * DriverManager类是驱动的管理类 利用DriverManager连接数据库,进行数据库驱动注册 * * @throws Exception */ @Test public void testDriverManager() throws Exception { String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // 读取properties文件 InputStream in = getClass().getClassLoader().getResourceAsStream( "jdbc.properties"); Properties properties = new Properties(); properties.load(in); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); Class.forName(driverClass); Connection connection = (Connection) DriverManager.getConnection( jdbcUrl, user, password); System.out.println(connection); } /** * 驱动测试,对链接驱动进行测试 * * @throws SQLException * */ @Test public void testDriver() throws SQLException { // 创建Drivers实现类 Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost:3306/jdbc"; Properties info = new Properties(); info.put("user", "root"); info.put("password", "root"); // 调用driver接口的Connection Connection connection = (Connection) driver.connect(url, info); System.out.println(connection); } /** * 编写通用方法获取任意数据库链接,不用修改源程序 * * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws SQLException * @throws IOException */ public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException { String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // 读取properties文件 InputStream in = getClass().getClassLoader().getResourceAsStream( "jdbc.properties"); Properties properties = new Properties(); properties.load(in); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); Driver driver = (Driver) Class.forName(driverClass).newInstance(); Properties info = new Properties(); info.put("user", user); info.put("password", password); Connection connection = (Connection) driver.connect(jdbcUrl, info); return connection; } @Test public void testConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException { System.out.println(getConnection()); }}
登录后复制
public class JDBCTools { /** * 结果查询关闭 * @param rs * @param statement * @param conn */ public static void release(ResultSet rs,Statement statement, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } } /** * 数据库更新方法 * @param sql */ public void uodate(String sql) { Connection connection = null; Statement statement = null; try { connection = JDBCTools.getConnection(); statement = (Statement) connection.createStatement(); statement.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.release(statement, connection); } } /** * 关闭数据库连接的方法 * @param statement * @param conn */ public static void release(Statement statement, Connection conn) { if (statement != null) { try { statement.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } } /** * 编写通用方法获取任意数据库链接,不用修改源程序 * * @return * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws SQLException * @throws IOException */ public static Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException { String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // 读取properties文件 InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream( "jdbc.properties"); Properties properties = new Properties(); properties.load(in); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); Driver driver = (Driver) Class.forName(driverClass).newInstance(); Properties info = new Properties(); info.put("user", user); info.put("password", password); Connection connection = (Connection) driver.connect(jdbcUrl, info); return connection; }}
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

在 Linux 中运行 MySQl(有/没有带有 phpmyadmin 的 podman 容器)

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么?
