2019-03-28 插入多条数据、连接数据库、控制台输入,与保

2019-03-28  本文已影响0人  北街九条狗

插入多条数据

-- 插入语句的一些补充
create table emp_copy4
AS
select  * from emp where sal=null
-- 子查询插入多条数据
INSERT into emp_copy4
select * from emp where deptno=20

INSERT into emp_copy4(empno,neme)
select empno,ename from emp where deptno=20

索引与视图

 索引 index -- 优点:加快查询速度;
--                           缺点:占内存,降低了增删改速度(因为索引表需要同步)
-- 主键自带索引
-- 经常需要作为条件的列最好建索引
create table test4(
id int(20) auto_increment,
name VARCHAR(20) default '' not NULL,
primary key (id),
INDEX(name)
)

-- 视图 view  命名的查询
-- 视图不存数据,存的是查询,视图是一个虚表
-- with check option-->视图可以查询到的数据,才能进行增删改
CREATE view emp_10
AS
select * from emp where  deptno=10

连接数据库

    public static void main(String[] args) {
        // 将声明放在外面,以便finally中关闭
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            // 1.加载数据库驱动程序,(需要把驱动加载到方法区)
            Class.forName("com.mysql.jdbc.Driver");
            //2.利用驱动管理器获取数据库连接
            //localhost=127.0.0.1 本地,如果需要连接他人数据,修改成对方的id
            // 3306 端口号  mysql默认端口号
            //java2demo  数据库名称
            //useUnicode=true&characterEncoding=utf8-->支持中文
            String url="jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8";
            conn = DriverManager.getConnection(url, "root", "root");
            //3.获取SQL语句
            String sql="select*from dept";
            ps=conn.prepareStatement(sql);
            //4.执行语句等到结果集
            rs= ps.executeQuery();
            while(rs.next()) {
                //编号
                int deptno=rs.getInt("deptno");
                //部门名称
                String dname=rs.getString("dname");
                //获取工作地点
                String loc=rs.getString("loc");
                System.out.println(deptno+","+dname+","+loc);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(conn !=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if( rs !=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps !=null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        
    }

控制台输入,与保密

public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入部门号");
        int a=scan.nextInt();
        Connection cnno=null;
        
        //Statement 是 PreparedStatement 的父类接口,只能执行静态接口
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8";
            cnno=DriverManager.getConnection(url,"root","root");
            String sql="select * from emp where deptno = ?";
            ps= cnno.prepareStatement(sql);
            ps.setInt(1, a);
            rs=ps.executeQuery();
            while(rs.next()) {
                int empno = rs.getInt("empno");
                String ename=rs.getString("ename");
                int sal=rs.getInt("sal");
                Date hiredate=rs.getDate("hiredate");
                System.out.println(empno +","+ename+","+sal+","+hiredate);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(cnno !=null) {
                try {
                    cnno.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(ps !=null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(rs !=null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            
        }

    }
上一篇 下一篇

猜你喜欢

热点阅读