JDBC实现基本的CRUD示例
private static void insertTest() throws SQLException
{
String dbURL = "jdbc:mysql://localhost/test";
Connection con = DriverManager.getConnection(dbURL, "root", "123");
Statement st = con.createStatement();
st.execute("insert into user(ID,NAME) values(1, 'Zhang San')");
st.execute("insert into user(ID,NAME) values(2, 'Li Si')");
st.execute("insert into user(ID,NAME) values(3, 'Wang Wu')");
System.out.println("=====insert test=====");
showUser(st);
st.close();
con.close();
} private static void deleteTest() throws SQLException
{
String dbURL = "jdbc:mysql://localhost/test";
Connection con = DriverManager.getConnection(dbURL, "root", "123");
Statement st = con.createStatement();
st.execute("delete from user where ID=3");
System.out.println("=====delete test=====");
showUser(st);
st.close();
con.close();
} private static void updateTest() throws SQLException
{
String dbURL = "jdbc:mysql://localhost/test";
Connection con = DriverManager.getConnection(dbURL, "root", "123");
Statement st = con.createStatement();
st.executeUpdate("update user set NAME='TEST' where ID=2");
System.out.println("=====update test=====");
showUser(st);
st.close();
con.close();
} private static void showUser(Statement st) throws SQLException
{
ResultSet rs = st.executeQuery("select ID, NAME from user");
while(rs.next())
{
int id = rs.getInt("ID");
String name = rs.getString("NAME");
System.out.println("ID:" + id + "; NAME=" + name);
}
rs.close();
}

最新文章

  1. ajax 后台正常执行 错误类型却是404
  2. 为WebDriver 设置proxy(IE设置代理)
  3. nginx 配置https upstream 跳转失败
  4. [AngularJS] 使用AngularAMD动态加载Service
  5. C# 刷新页面浏览次数(点击量)+1
  6. PS 的参考线
  7. LICEcap 简洁易用的动画屏幕录制软件
  8. 装有Win7系统的电脑在局域网不能共享的解决方案
  9. Learn Objectvie-C on the Mac 2nd Edition 笔记
  10. HTTP协议和WEB应用
  11. css+div布局案例
  12. 用《内网穿山甲》把本地IIS中的站点共享到远程访问
  13. PHP获取远程文件的几种方式
  14. Visual studio 2017 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
  15. 学习使用Mendeley1
  16. VUE插件总结
  17. 20155324 2016-2017-2 《Java程序设计》第3周学习总结
  18. Android Day2
  19. Ajax进阶之原生js与跨域jsonp
  20. Linux中2>&1使用

热门文章

  1. 不使用Tomcat,手写简单的web服务
  2. Java——Iterate through a HashMap
  3. 重复代码Duplicated Code---要重构的信号
  4. bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 ——中位数排序
  5. spring-retry 重试机制
  6. python自动开发之第二十五天
  7. Linux内核基础--事件通知链(notifier chain)good【转】
  8. (八)hope
  9. 设计模式之笔记--建造者模式(Builder)
  10. (转)opencv 代替caffe.io.load_image