PreparedStatement:

方法:


Connection:

方法:



 实例:


1、查询:

 package cn.chuang.JdbcDome;

 import java.sql.*;

 public class JdbcDome3 {
public static void main(String[] args) throws Exception {
PreparedStatement ppst = null;
Connection conn = null;
fun3(ppst,conn);
} public static void fun1(PreparedStatement ppst,Connection conn) throws Exception {
//查询表的内容
//1 注册驱动 获得Connection
Class.forName("com.mysql.jdbc.Driver");
//2 获得链接
conn = DriverManager.getConnection("jdbc:mysql:///semployee", "root", "root");
//3 sql语句
String sql = "select * from lll";
//4 获得执行sql语句的对象
ppst = conn.prepareStatement(sql);
ResultSet rs = ppst.executeQuery(sql);
//5 让游标向下移动一行
rs.next();
int i = rs.getInt(1);
String name = rs.getString("ename");
//6 获取数据
System.out.println(i+" "+name);
}

2、添加

     public static void fun2(PreparedStatement ppst,Connection conn) throws Exception {
//在表中添加数据,表结构有多少就要写多少。不能漏写,会报错。
try {
//1 注册驱动 获得Connection
Class.forName("com.mysql.jdbc.Driver");
//2 获得链接
conn = DriverManager.getConnection("Jdbc:mysql:///semployee", "root", "root"); //3 sql语句
String sql = "insert into lll values (null,'兀立扗'),(null,'吴诗意')";
//4 获得执行sql语句的对象
ppst = conn.prepareStatement(sql);
int i = ppst.executeUpdate(sql);
//5 处理结果
System.out.println(i);
//6 另创建if语句,做提示用。
if (i>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(ppst!=null){
try {
ppst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

3、删除 

 public static void fun3(PreparedStatement ppst,Connection conn) throws Exception {
//删除表内数据。
//1 注册驱动 获得Connection
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///semployee", "root", "root");
//2 sql语句
String sql = "delete from lll where uid = 2";
//3 获得执行sql语句的对象Statement
ppst = conn.prepareStatement(sql);
int i = ppst.executeUpdate(sql);
//4 处理结果
System.out.println(i); if (i>0){
System.out.println("删除成功");
ppst.close();
conn.close();
}else{
System.out.println("删除失败");
}
}

 4、修改

 public static void fun4(PreparedStatement ppst,Connection conn) throws Exception {
      //修改表内数据
//1 注册驱动。
Class.forName("com.mysql.jdbc.Driver");
//2 链接数据库。
conn = DriverManager.getConnection("jdbc:mysql:///semployee", "root", "root"); //3 SQL语句。
String sql = "update lll set uname = '吴惆' where uid = 1 "; //4 获得执行SQL的语句。
ppst = conn.prepareStatement(sql);
//5 处理结果。
int ou = ppst.executeUpdate(sql);
System.out.println(ou);
}
}

最新文章

  1. 用神奇的currentColor制作简洁的颜色动画效果
  2. Ext.Net TextField Enter事件
  3. Android之ListView/GridView 优化
  4. 20141014C#面向对象抽象方法,抽象类,接口
  5. Oracle的正则应用之匹配出手机号
  6. C++ Primer Plus 6 第一章
  7. Swift基础之实现一个镂空图片的小Demo
  8. openlayers4 入门开发系列之地图模态层篇(附源码下载)
  9. django后台对某些字段设置颜色
  10. 3D印表機 零件採購資訊
  11. centos7之安装wordpress
  12. 第十四章、Linux 账号管理与 ACL 权限配置
  13. group by的运用
  14. 永远不要去B网(Bittrex.com)
  15. Scrapy之CrawlSpider
  16. 关于java中的编码问题
  17. Java接口测试之使用有道翻译API
  18. 集合Set、List、Map的遍历方法
  19. nyoj--1170--最大的数(数学技巧)
  20. Navicat Premium基本使用

热门文章

  1. ViewPager 和 SwipeRefreshLayout 的滑动冲突
  2. FOR xml path 这么爽的SQL命令,居然今天才知道
  3. python标准库-array 模块
  4. moment获取2周后日期
  5. 中文字体压缩器-解决css引入的字体太大的问题
  6. kNN算法 Demo
  7. 全栈新视觉——前后端分离
  8. poi简介
  9. iOS 使用系统的UITabBarController 修改展示的图片大小
  10. JZOJ 1736. 扑克游戏 (Standard IO)