1、添加

package pers.Pre.add;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Add {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String insertSql = "insert into score(studentno,courseno,usually,final)values(?,?,?,?)";
            ps = con.prepareStatement(insertSql);// 获取预处理对象
            ps.setString(, ");
            ps.setString(, "b221");
            ps.setDouble(, );
            ps.setDouble(, );
            int num = ps.executeUpdate();
            System.out.println("添加了" + num + "条记录");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

2、删除

package pers.Pre.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DeleteDemo {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String deleteSql = "Delete from Student where sname=?";
            ps = con.prepareStatement(deleteSql);// 获取预处理对象
            ps.setString(,"秀儿");
            int num = ps.executeUpdate();
            System.out.println("删除了" + num + "条信息");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}

3、修改

package pers.Pre.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class UpdateDemo {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String updateSql = "Update Login set password=? where account ='学前班秀儿'";
            ps = con.prepareStatement(updateSql);// 获取预处理对象
            ps.setString(,");
            int num = ps.executeUpdate();
            System.out.println("更改了" + num + "条信息");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}

4、查询

package pers.Pre.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SelectDemo {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String selectSql = "select * from score";
            ps = con.prepareStatement(selectSql);// 获取预处理对象
            rs = ps.executeQuery();
            System.out.println("     "+"学号"+"                        "+"班级"+"           "+"平时成绩"+"    "+"期末成绩");
            while (rs.next()) {

                System.)+)+)+"  "+
                        rs.getString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
                if (rs!=null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}

5、Statement接口与PreparedStatement接口的对比

Statement接口用于处理不带参数的静态sql语句,PreparedStatement接口可以处理带参数的sql语句。

增删改查——PreparedStatement接口的更多相关文章

  1. 增删改查——Statement接口

    1.增加数据表中的元组 package pers.datebase.zsgc; import java.sql.Connection; import java.sql.DriverManager; i ...

  2. 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类

    在上一篇文章中<使用DbUtils实现增删改查>,发现运行runner.query()这行代码时.须要自己去处理查询到的结果集,比較麻烦.这行代码的原型是: public Object q ...

  3. 简单实现 nodejs koa2 mysql 增删改查 制作接口

    1.首先 在电脑上安装 nodejs (此处略过) 2.全局安装 koa2 (这里使用的淘宝镜像cnpm,有兴趣的同学可以自行搜索下) cnpm install koa-generator -g 3. ...

  4. jdbc的连接数据库,使用PreparedStatement实现增删改查等接口

    首先是连接,关闭资源等数据库操作 将连接数据库,关闭资源封装在JDBCUtils里 package jdbc.utils; import java.sql.Connection; import jav ...

  5. 使用JDBC分别利用Statement和PreparedStatement来对MySQL数据库进行简单的增删改查以及SQL注入的原理

    一.MySQL数据库的下载及安装 https://www.mysql.com/ 点击DOWNLOADS,拉到页面底部,找到MySQL Community(GPL)Downloads,点击 选择下图中的 ...

  6. 如何用tep完成增删改查接口自动化

    tep的设计理念是让人人都可以用Python写自动化,本文就来介绍如何用tep完成增删改查接口自动化. 环境变量 编辑fixtures/fixture_admin.py: "qa" ...

  7. hibernate实现增删改查的各种方法

    1>接口(主要是增删改查的接口)BaseDao.java /** * * @author fly.zhou */ public interface IBaseDao { //增加对应实体的一条记 ...

  8. VUE2.0增删改查附编辑添加model(弹框)组件共用

    Vue实战篇(增删改查附编辑添加model(弹框)组件共用) 前言 最近一直在学习Vue,发现一份crud不错的源码 预览链接 https://taylorchen709.github.io/vue- ...

  9. Mock.js简易教程,脱离后端独立开发,实现增删改查功能(转)

    在我们的生产实际中,后端的接口往往是较晚才会出来,并且还要写接口文档,于是我们的前端的许多开发都要等到接口给我们才能进行,这样对于我们前端来说显得十分的被动,于是有没有可以制造假数据来模拟后端接口呢, ...

随机推荐

  1. hadoop2.7之作业提交详解(上)

    根据wordcount进行分析: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; impo ...

  2. 使用 php 内部web服务器

    使用 php 内部web服务器如网站目录 d:\web\index.php1.打开命令窗口,输入下列3条命令cd d:cd d:\web\index.phpphp -S localhost:80802 ...

  3. ORACLE中添加删除主键

    本文转自:http://blog.chinaunix.net/uid-17079336-id-2832443.html 1.创建表的同时创建主键约束(1)无命名create table student ...

  4. C++实现微信WeChat网页接口推送股票报警消息

    QStockView微信推送股票报警 1.功能简介 最近很多用户反馈,软件只能在电脑上使用,不能在手机上使用.所以增加了微信推送报警的功能,电脑端的报警提示消息可以通过微信同步发送到手机微信.这样即可 ...

  5. Oracle中的字符函数

    Oracle中常用的字符串函数有以下几种: 1.upper()---将字符串的内容全部转换为大写.lower()---将字符串的内容全部转换为小写.具体用法: select  upper('test' ...

  6. pip安装第三方库

    不是所有的第三方Python包都能通过pip来安装,只能是发布在pypi.org上面的才能通过pip安装. pypi是什么? pypi是一个仓库,上面存放了大量的Python第三方软件包,是由Pyth ...

  7. Springboot源码分析之代理三板斧

    摘要: 在Spring的版本变迁过程中,注解发生了很多的变化,然而代理的设计也发生了微妙的变化,从Spring1.x的ProxyFactoryBean的硬编码岛Spring2.x的Aspectj注解, ...

  8. day0203

    day02 1.for i in range() --->用于设置for循环的迭代设置. ranage 也是一个前闭后开的. 2.random.randrange() --->随机产生给予 ...

  9. Qt最新版5.12.2在Win10环境静态编译安装和部署的完整过程(VS2017)

    一.为什么要静态编译 用QtCreator编译程序时,使用的是动态编译.编译好的程序在运行时需要另外加上相应的Qt库文件,一大堆dll文件.如果想将生成的程序连同所需要的库一起完整地打包成一个可执行程 ...

  10. Egret白鹭开发小游戏中容易犯的错

    在游戏开发过程中遇到问题,请首先查阅:http://developer.egret.com/cn/github/egret-docs/Engine2D/minigame/minigameFAQ/ind ...