0

0

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

php中文网

php中文网

发布时间:2016-06-01 13:07:53

|

1142人浏览过

|

来源于php中文网

原创

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;	}}



SuperCms在线订餐系统
SuperCms在线订餐系统

模板采用响应式设计,自动适应手机,电脑及平板显示;满足单一店铺外卖需求。功能:1.菜单分类管理2.菜品管理:菜品增加,删除,修改3.订单管理4.友情链接管理5.数据库备份6.文章模块:如:促销活动,帮助中心7.单页模块:如:企业信息,关于我们更强大的功能在开发中……安装方法:上传到网站根目录,运行http://www.***.com/install 自动

下载
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

56

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

30

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

396

2026.01.21

妖精漫画入口地址合集
妖精漫画入口地址合集

本专题整合了妖精漫画入口地址合集,阅读专题下面的文章了解更多详细内容。

118

2026.01.21

java版本选择建议
java版本选择建议

本专题整合了java版本相关合集,阅读专题下面的文章了解更多详细内容。

3

2026.01.21

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

11

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

50

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号