• MySql用statement实现DDL,DML,DQL的操作Demo


    Demo1

    Connection connection=null;
            Statement stmt=null;
            int result=-1;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
                
            } catch (ClassNotFoundException e) {
    
                e.printStackTrace();
            }
            
            
            try {
                //创建连接
                String url="jdbc:mysql://localhost:3306/jdbcdb";
                connection= DriverManager.getConnection(url, "root", "mysql");
            } catch (SQLException e) {
    
                e.printStackTrace();
            }
            
            try {
                //创建Statement
                String sql="CREATE TABLE s_user(id INT PRIMARY KEY AUTO_INCREMENT,    NAME VARCHAR(20),PASSWORD VARCHAR(15)) ";
                 stmt=connection.createStatement();
                 //执行sql语句,返回受影响行数 ————int值
                 result= stmt.executeUpdate(sql);
            } catch (SQLException e) {
    
                e.printStackTrace();
            }
            
            System.out.println("result="+result);
            
            try {
                //关闭流
                if(stmt!=null)
                {
                    stmt.close();
                }
                
                if(connection!=null)
                {
                    connection.close();
                }
                
            } catch (SQLException e) {
    
                e.printStackTrace();
            }

    Demo2

    Connection connection=null;
            Statement stmt=null;
            ResultSet rSet=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/jdbcdb";
                String user="root";
                String password="mysql";
                //连接
                connection= DriverManager.getConnection(url, user, password);
                stmt= connection.createStatement();//statement
                String sql="SELECT * from s_user;";
                rSet= stmt.executeQuery(sql);//执行sql语句---数据集(类似于map)
                
                while (rSet.next()) {
                    //获取值(通过索引)
                    int id= rSet.getInt(1);
                    String name=rSet.getString(2);
                    String pwd=rSet.getString(3);
                    
                    System.out.println("id="+id+";name="+name+";pwd="+pwd);
                    //通过行列号
                    id=rSet.getInt("id");
                    name=rSet.getString("name");
                    pwd=rSet.getString("password");
                    
                    System.out.println("~~~~~~~id="+id+";name="+name+";pwd="+pwd);
                    
                }
                
            } catch
  • 相关阅读:
    vim tail
    范式
    $@疑点
    ^ $ 和 a z 字符匹配
    [导入]DataList编辑、更新、取消、删除、分页(分页控件AspNetPager.dll)
    [导入]用.net操作word
    导出文件
    [导入]总结:ADO.NET在开发中的部分使用方法和技巧
    读取DataTable中的数据,一行一行进行比较
    [导入]ASP.NET 数据访问类
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4095994.html
Copyright © 2020-2023  润新知