• MyEclipse 连接Oracle数据库(初学者必看)


    前言:刚接触Oracle数据库,便有一个需求,编写控制台程序,实现主人登录。数据库为Oracle。下面详细介绍一下MyEclipse 连接Oracle数据库。
     
    package DbHelp;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    import oracle.jdbc.driver.OracleDriver;
    
    /**
     * 数据库连接帮助类
     * @author zql_pc
     *
     */
    public class DbHelp {
    
        /*
         * 数据库连接
         */
        public static Connection lianjie(){
            
            //数据库配置
            String driver = "oracle.jdbc.driver.OracleDriver";//驱动
            String url = "jdbc:oracle:thin:@localhost:1521:JBITDB";
            String user = "epet";
            String pwd = "123456";
            
            try {
                //第一步:加载驱动
                Class.forName(driver);// -->反射
                
                Connection conn = DriverManager.getConnection(url,user,pwd);
                
                return conn;
                
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return null;
        }
    }
    /**
     * 宠物实体类
     * @author zql_pc
     *
     */
    public class pet {
    
        private int id;
    
        private String name;
        private int health;
        private int love;
        private String type;
        private String adoptTime;
        
        public pet(){
            
        }
        
        public pet(int id, String name, int health, int love, String type,
                String adoptTime) {
            super();
            this.id = id;
            this.name = name;
            this.health = health;
            this.love = love;
            this.type = type;
            this.adoptTime = adoptTime;
        }
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getHealth() {
            return health;
        }
        public void setHealth(int health) {
            this.health = health;
        }
        public int getLove() {
            return love;
        }
        public void setLove(int love) {
            this.love = love;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getAdoptTime() {
            return adoptTime;
        }
        public void setAdoptTime(String adoptTime) {
            this.adoptTime = adoptTime;
        }
    
        @Override
        public String toString() {
            return "pet [adoptTime=" + adoptTime + ", health=" + health + ", id="
                    + id + ", love=" + love + ", name=" + name + ", type=" + type
                    + "]";
        }
        
        
    }
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    import org.junit.Test;
    
    import DbHelp.DbHelp;
    /**
     * 登陆
     * @author zql_pc
     *
     */
    public class login {
        
        @Test
        public void show(){
            Scanner input = new Scanner(System.in);
            
            //调用DbHelp类的连接数据库方法获取connection对象
            Connection conn = DbHelp.lianjie();
            
            System.out.println("----欢迎光临宠物乐园----");
            System.out.print("请输入登录名:");
            
            //控制台输入的用户名
            String name = input.next();
            
            System.out.print("请输入密码:");
            
            //控制台输入的密码
            String pwd = input.next();
            
            //sql
            String sql = "select * from master where loginid= ?  and password = ?";
            
            PreparedStatement pst = null;
            
            ResultSet rs = null;
            
            //预编译操作数据库
            try {
                pst =  conn.prepareStatement(sql);
                
                pst.setString(1,name);
                pst.setString(2, pwd);
                
                rs = pst.executeQuery();
                
                if(rs.next()){
                    System.out.println("登陆成功!");
                }else{
                    System.out.println("登陆失败!");
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Scanner;
    
    import org.junit.Test;
    
    import DbHelp.DbHelp;
    
    /**
     * 领养宠物类
     * @author zql_pc
     *
     */
    public class adoptPet {
    
        Scanner input = new Scanner(System.in);
        
        
        public void show() throws SQLException{
            
            System.out.println("----欢迎光临宠物乐园----");
            
            //获取数据库连接
            Connection conn = DbHelp.lianjie();
            
            System.out.print("请输入要领养的宠物名字:");
            String name = input.next();
            
            System.out.println("请输入领养宠物类型(1、狗狗  2、企鹅):");
            int type = input.nextInt();
            
            String sql = "insert into pet values(4,1,?,?,100,100,null,null,null,to_date('2015-1-5','yyyy-mm-dd'),1)";
            
            PreparedStatement pst = conn.prepareStatement(sql);
            
            pst.setString(1, name);
            pst.setInt(2, type);
            
            int num = pst.executeUpdate();
            
            if(num>0){
                System.out.println("领养成功!");
            }else{
                System.out.println("领养失败!!");
            }
            
            //关闭省
        }
        
        //打印狗狗信息
        @Test
        public void show1(){
            
            Connection conn = DbHelp.lianjie();
            Statement st =null;
            List<pet> list = null;
            try {
                st = conn.createStatement();
                
                String sql = "select id,name,health,love,decode(type_id,1,'正常',2,'禁用'),to_char(adopt_time,'yyyy-mm-dd') from pet";
                
                ResultSet rs = st.executeQuery(sql);
                
                list = new ArrayList<pet>();
                
                while(rs.next()){
                    pet pet = new pet(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5),rs.getString(6));
                    list.add(pet);
                }
                
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            System.out.println("----主人领养的所有宠物类型----");
            System.out.println("ID	宠物名	健康值	亲密度	状态	领养时间");
            System.out.println("-----------------------------------------");
            
            Iterator<pet> i = list.iterator();
            
            while(i.hasNext()){
                
                pet pet1 = i.next();
                
                System.out.println(pet1.getId()+"	"+pet1.getName()+"	"+pet1.getHealth()+"	"+pet1.getLove()+"	"+pet1.getType()+"	"+pet1.getAdoptTime());
                
            }
            
        }
    }

     
     
     
     
  • 相关阅读:
    python 实现redis订阅发布功能
    python装饰器实现对异常代码出现进行监控
    回首2017 展望2018
    结合jira搭建自动化测试平台
    安装YApi 接口管理平台
    Django 连接mysql数据库中文乱码
    在django admin中添加自定义视图
    django 模型models
    用户行为分析数据库设计
    vCenter Server 6 Standard
  • 原文地址:https://www.cnblogs.com/wlx520/p/4483046.html
Copyright © 2020-2023  润新知