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();
        }
  • 相关阅读:
    Eclipse SVN插件设置
    经典语录-每日积累-05
    Shell基础语法,运算符,循环和判断语句和设置启动参数
    iOS-Jenkins自动化打包集成
    App版本升级相关
    Java-数组和集合简单使用
    Java-内部类简单使用
    Callkit被拒
    Java-Finalize(GC)和类与类和接口之间的关系
    经典语录-每日积累-04
  • 原文地址:https://www.cnblogs.com/baisha/p/15463717.html
Copyright © 2011-2022 走看看