zoukankan      html  css  js  c++  java
  • 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());
                
            }
            
        }
    }

     
     
     
     
  • 相关阅读:
    2012-2013年度大总结
    [每日一题] 11gOCP 1z0-052 :2013-08-31 数据库的存储结构....................................................A8
    Oracle约束操作
    几道字典树题目
    1032
    uva 10718 Bit Mask(贪心)
    找工作笔试面试那些事儿(2)---内存管理那些事
    汉语-词语:结构
    汉语-词语:形式
    汉语-词语:方向
  • 原文地址:https://www.cnblogs.com/wlx520/p/4483046.html
Copyright © 2011-2022 走看看