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();
                }
            }
        }
     
     
    }
  • 相关阅读:
    数据驱动编程法
    23个设计模式的简明教程
    分享一篇文章C语言字节对齐问题(适用于C++)转载至http://blog.csdn.net/21aspnet/article/details/6729724
    关于C++类中访问权限的若干疑问(虚函数访问权限)
    利用C# 反射设计支持可扩展插件的应用程序
    隐藏控制台console application窗口
    Intellij IDEA社区版上新建项目或模块没有Spring Initializr选项解决办法
    mac jmeter 界面乱码
    windows 查看端口被占用进程
    php static 变量声明
  • 原文地址:https://www.cnblogs.com/9635741zly/p/14916690.html
Copyright © 2011-2022 走看看