MySql执行JDBC联接(增/删/改/查)操作_MySQL

php中文网
发布: 2016-06-01 13:01:44
原创
2085人浏览过

视频地址:http://www.tudou.com/programs/view/4gienz1qdp0/

新建BaseDao

图改改
图改改

在线修改图片文字

图改改 455
查看详情 图改改
package cn.wingfly.dao;

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

public class BaseDao {
	Connection con = null;
	Statement st = null;
	ResultSet rs = null;

	/**
	 * 获得联接
	 * 
	 * @return
	 */
	public Connection getConnection() {
		try {
			// 加载驱动,这一句也可写为:Class.forName("com.mysql.jdbc.Driver"); 
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			// 建立到MySQL的连接 
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/money_note?characterEncoding=UTF-8", "root", "root");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	/**
	 * 关闭数据源
	 */
	public void CloseConnection(Connection con, Statement s, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (s != null) {
				s.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
登录后复制
测试类
package cn.wingfly.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test extends BaseDao {

	Connection con = null;
	Statement st = null;
	ResultSet rs = null;

	/**
	 * 查询数据
	 */
	public void find() {
		con = getConnection();	// 获得联接
		try {
			st = con.createStatement();
			rs = st.executeQuery("select * from app_user");
			while (rs.next()) {
				System.out.println("编号:" + rs.getInt("uuid") + ", 姓名:" + rs.getString("userName") + ", 性别:" + rs.getString("sex") + ", 生日:" + rs.getString("birthday") + ", 住址:" + rs.getString("address"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);	// 关闭联接
		}
	}

	/**
	 * 添加数据
	 */
	public void add() {
		con = getConnection();
		try {
			st = con.createStatement();
			int result = st.executeUpdate("insert into app_user(userName,passWord,sex,birthday,address) values('赵丽颖','wanying','女','1992-02-03','北京市')");
			if (result > 0) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			System.out.println("插入失败");
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);
		}
	}

	/**
	 * 更新数据
	 */
	public void update() {
		con = getConnection();
		try {
			st = con.createStatement();
			int result = st.executeUpdate("update app_user set address = '河南' where uuid = '3'");
			if (result > 0) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);
		}
	}

	/**
	 * 删除数据
	 */
	public void delete() {
		con = getConnection();
		try {
			st = con.createStatement();
			int result = st.executeUpdate("delete from app_user where uuid = '3'");
			if (result > 0) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);
		}
	}

	public static void main(String[] args) {
		Test test = new Test();
		//test.add();
		// test.update();
		 test.delete();
		test.find();

	}

}
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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