zoukankan      html  css  js  c++  java
  • JDBC添加数据

    package com.atguigu.connection;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    
    public class ConnectionTest {
    
        
        public static void main(String[] args) {
            
            Connection conn = null;
            Statement stmt = null;
            String sql = null;
            Scanner sr = new Scanner(System.in);
            System.out.println("请输入等级:");
            String level = sr.next();
            System.out.println("请输入最低工资:");
            int min_sal = sr.nextInt();
            System.out.println("请输入最高工资:");
            int max_sal = sr.nextInt();
            try {
                //1.加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                //2.创建连接对象
                 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myemployees","root", "123456890");
                //3.创建statement对象
                 stmt = conn.createStatement();
                //4.执行SQL语句
                 sql = "INSERT INTO `myemployees`.`job_grades`(`grade_level`,`lowest_sal`,`highest_sal`)VALUES ('"+level+"','"+min_sal+"','"+max_sal+"')";
                int row = stmt.executeUpdate(sql);
                if(row>0) {
                    System.out.println("执行成功");
                }else {
                    System.out.println("执行失败");
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if(stmt!=null) {
                    stmt.close();
                    }
                    if(conn!=null&&conn.isClosed()==false) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            
        }
        
        
    }
  • 相关阅读:
    字符串与Unicode码的相互转换
    关于es6中的yield
    ajax请求中的6个全局事件
    用H5上传文件
    类型化数组
    git笔记-9-29
    js正则表达式验证身份证号和密码
    assertThat用法
    java产生随机数的几种方式
    jQuery之Deferred对象详解
  • 原文地址:https://www.cnblogs.com/de-ming/p/13581914.html
Copyright © 2011-2022 走看看