zoukankan      html  css  js  c++  java
  • 数据库的修改

    import java.sql.*;

     
    import java.util.Scanner;
     
    public class jdbcupdate {
        public static void main(String[]args) {
            final String URL = "jdbc:mysql://localhost:3306/test";
            final String USERNAME = "root";
            final String PWD = "12345";
            Connection connection = null;
            Statement  stmt = null;
            Scanner con=new Scanner(System.in);
            String idnumber;
            String classname;
            String teachername;
            String didian;
             
            //idnumber=con.nextLine();
            classname=con.nextLine();
            teachername=con.nextLine();
            //didian=con.nextLine();
            try {
                // a.导入驱动,加载具体的驱动类
                Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
                // b.与数据库建立连接
                connection = DriverManager.getConnection(URL, USERNAME, PWD);
                stmt = connection.createStatement();
                 
                 
                //删除时如果是中文汉字要加单引号,如果是变量的话需要双引号单引号双引号
                String sql = "update student set teacher='"+teachername+"' where classname='"+classname+"'";
                // 执行SQL
                int count = stmt.executeUpdate(sql);
                 
                 
                 
                // d.处理结果
                if (count > 0) { 
                    System.out.println("操作成功!");
                }
            catch (ClassNotFoundException e) {
                e.printStackTrace();
            catch (SQLException e) {
                e.printStackTrace();
            catch(Exception e) {
                e.printStackTrace();
            }
            finally {
                try {
                     if(stmt!=null) stmt.close();// 对象.方法
                     if(connection!=null)connection.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        }
     
     
    }
  • 相关阅读:
    Codeforces Round #308 (Div. 2) A B C 水 数学
    2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869
    Mysql5.7 安装
    NTP服务器时间同步
    postgres-xl 集体搭建(2)
    postgres-xl 集体搭建(1)
    postgres-xl 集体搭建
    单词本
    docker machine
    桥接 NAT HOST-ONLY
  • 原文地址:https://www.cnblogs.com/9635741zly/p/14916681.html
Copyright © 2011-2022 走看看