首页 数据库 mysql教程 Java链接mySQL数据库进行增删改查_MySQL

Java链接mySQL数据库进行增删改查_MySQL

Jun 01, 2016 pm 01:07 PM

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

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

与MySQL中使用索引相比,全表扫描何时可以更快? 与MySQL中使用索引相比,全表扫描何时可以更快? Apr 09, 2025 am 12:05 AM

全表扫描在MySQL中可能比使用索引更快,具体情况包括:1)数据量较小时;2)查询返回大量数据时;3)索引列不具备高选择性时;4)复杂查询时。通过分析查询计划、优化索引、避免过度索引和定期维护表,可以在实际应用中做出最优选择。

说明InnoDB全文搜索功能。 说明InnoDB全文搜索功能。 Apr 02, 2025 pm 06:09 PM

InnoDB的全文搜索功能非常强大,能够显着提高数据库查询效率和处理大量文本数据的能力。 1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。 2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。 3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

可以在 Windows 7 上安装 mysql 吗 可以在 Windows 7 上安装 mysql 吗 Apr 08, 2025 pm 03:21 PM

是的,可以在 Windows 7 上安装 MySQL,虽然微软已停止支持 Windows 7,但 MySQL 仍兼容它。不过,安装过程中需要注意以下几点:下载适用于 Windows 的 MySQL 安装程序。选择合适的 MySQL 版本(社区版或企业版)。安装过程中选择适当的安装目录和字符集。设置 root 用户密码,并妥善保管。连接数据库进行测试。注意 Windows 7 上的兼容性问题和安全性问题,建议升级到受支持的操作系统。

InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 Apr 02, 2025 pm 06:25 PM

聚集索引和非聚集索引的区别在于:1.聚集索引将数据行存储在索引结构中,适合按主键查询和范围查询。2.非聚集索引存储索引键值和数据行的指针,适用于非主键列查询。

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么? 哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么? Mar 21, 2025 pm 06:28 PM

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

您如何处理MySQL中的大型数据集? 您如何处理MySQL中的大型数据集? Mar 21, 2025 pm 12:15 PM

文章讨论了处理MySQL中大型数据集的策略,包括分区,碎片,索引和查询优化。

mysql:简单的概念,用于轻松学习 mysql:简单的概念,用于轻松学习 Apr 10, 2025 am 09:29 AM

MySQL是一个开源的关系型数据库管理系统。1)创建数据库和表:使用CREATEDATABASE和CREATETABLE命令。2)基本操作:INSERT、UPDATE、DELETE和SELECT。3)高级操作:JOIN、子查询和事务处理。4)调试技巧:检查语法、数据类型和权限。5)优化建议:使用索引、避免SELECT*和使用事务。

mysql用户和数据库的关系 mysql用户和数据库的关系 Apr 08, 2025 pm 07:15 PM

MySQL 数据库中,用户和数据库的关系通过权限和表定义。用户拥有用户名和密码,用于访问数据库。权限通过 GRANT 命令授予,而表由 CREATE TABLE 命令创建。要建立用户和数据库之间的关系,需创建数据库、创建用户,然后授予权限。

See all articles