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();
                }
            }
            
            
        }
        
        
    }
  • 相关阅读:
    css中的元素旋转
    display:inlineblock的深入理解
    js时间获取。
    长英文自动换行的最终解决方法
    jqery图片展示效果
    链接A引发的思考
    电子邮件制作规范和建议
    overflow与textindent:9999px 字体隐藏及input value偏移
    jQuery load的详解
    转载:前端调试利器DebugBa
  • 原文地址:https://www.cnblogs.com/de-ming/p/13581914.html
Copyright © 2011-2022 走看看