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();
        }
  • 相关阅读:
    nodejs + typescirpt + vs code
    NodeJs使用nodejs-websocket + protobuf
    Windows10环境下使用VisualSVN server搭建SVN服务器
    微信小游戏下socket.io的使用
    JS中实现种子随机数
    帧同步和状态同步
    EgretPaper学习笔记一 (安装环境,新建项目)
    反编译微信小游戏
    微信小游戏 小程序跳转修改 不支持动态更新,只能在发布时修改
    HTML5实现本地JSON文件的读写
  • 原文地址:https://www.cnblogs.com/baisha/p/15463717.html
Copyright © 2011-2022 走看看