zoukankan      html  css  js  c++  java
  • JDBC_Statement

    插入数据

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException, IOException {
            InputStream is = Jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.propertise");
            Properties properties = new Properties();
            properties.load(is);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driver = properties.getProperty("driver");
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,user,password);
            Statement statement = conn.createStatement();
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入账户");
            String username = sc.next();
            System.out.println("请输入密码");
            String userpassword = sc.next();
            String sql = "insert into emp values(default,'"+username+"','"+userpassword+"')";
            statement.execute(sql);
            statement.close();
            conn.close();
        }

    查询数据

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException, IOException {
            //加载配置文件
            InputStream is = Jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.propertise");
            Properties properties = new Properties();
            properties.load(is);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driver = properties.getProperty("driver");
            //连接数据库
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,user,password);
            //查询结果
            Statement statement = conn.createStatement();
            String sql = "select * from emp;"; //SQL查询语句
            ResultSet rs = statement.executeQuery(sql); //执行查询语句
            //打印结果
            while(rs.next()){
                System.out.println(rs.getString("user")+"    "+
                        rs.getString("password"));
            }
            //关闭对象
            rs.close();
            statement.close();
            conn.close();
        }
  • 相关阅读:
    jQuery中jsonp的跨域处理,no access-control-allow-origin,unexpected token
    doT中嵌套for循环的使用
    c++ new带括号和不带括号
    python装饰器之使用情景分析
    Python中classmethod与staticmethod区别
    python作用域 scope
    duck type鸭子类型
    EAFP和LBYL 两种防御性编程风格
    c++重载、覆盖和隐藏
    c++ 名字粉碎(name mangling)
  • 原文地址:https://www.cnblogs.com/baisha/p/15463717.html
Copyright © 2011-2022 走看看