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\t宠物名\t健康值\t亲密度\t状态\t领养时间");
        System.out.println("-----------------------------------------");
        
        Iterator<pet> i = list.iterator();
        
        while(i.hasNext()){
            
            pet pet1 = i.next();
            
            System.out.println(pet1.getId()+"\t"+pet1.getName()+"\t"+pet1.getHealth()+"\t"+pet1.getLove()+"\t"+pet1.getType()+"\t"+pet1.getAdoptTime());
            
        }
        
    }
}

 
 
 
 

转载于:https://www.cnblogs.com/wlx520/p/4483046.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值