eclipse下通过jdbc连接各个类型的数据库

5 篇文章 0 订阅
2 篇文章 0 订阅
以下是写在config.properties文件里面的内容(除了加的中文注释):
OracleDriverName=oracle.jdbc.driver.OracleDriver
OracleUrl=jdbc:oracle:thin:@127.0.0.1:1521:xe    xe为oracle数据库的SID,每个人的都不一样,要修改成自己的
OracleUserName=
OraclePassword=
MysqlDriverName=com.mysql.jdbc.Driver
MysqlUrl=jdbc:mysql://127.0.0.1:3306      
可以在3306后加上/要连接的数据库名称,也可以不写,不过在sql语句上要写成:数据库名.表名    
MysqlUserName=
MysqlPassword=
maxActive=3    最大连接数量,用于配置数据库连接池
maxWait=5000   最大等待时间毫秒
pulbic class jdbcUtil{
    private static BasicDataSource ds;
    private static BasicDataSource ds1;
    static {
        try {
            //加载config.properties文件
            Properties prop=new Properties();
            prop.load(new FileInputStream("config.properties"));
            //获取信息初始化属性
            String OracleDriverName=prop.getProperty("OracleDriverName");//驱动名
            String OracleUrl=prop.getProperty("OracleUrl");//地址值
            String OracleUserName=prop.getProperty("OracleUserName");//用户名
            String OraclePassword=prop.getProperty("OraclePassword");//密码

            String MysqlDriverName=prop.getProperty("MysqlDriverName");
            String MysqlUrl=prop.getProperty("MysqlUrl");
            String MysqlUserName=prop.getProperty("MysqlUserName");
            String MysqlPassword=prop.getProperty("MysqlPassword");

            int maxActive=Integer.parseInt(prop.getProperty("maxActive"));//最大连接数量
            int maxWait=Integer.parseInt(prop.getProperty("maxWait"));//最大等待时间
            //初始化oracle连接池
            ds=new BasicDataSource();
            ds.setDriverClassName(OracleDriverName);
            ds.setUrl(OracleUrl);
            ds.setUsername(OracleUserName);
            ds.setPassword(OraclePassword);
            ds.setMaxActive(maxActive);
            ds.setMaxWait(maxWait);
            //初始化mysql连接池
            ds1=new BasicDataSource();
            ds1.setDriverClassName(MysqlDriverName);;
            ds1.setUrl(MysqlUrl);
            ds1.setUsername(MysqlUserName);
            ds1.setPassword(MysqlPassword);
            ds1.setMaxActive(maxActive);
            ds1.setMaxWait(maxWait);
        } catch (Exception e) {
            System.out.println("数据库信息初始化失败!");
            e.printStackTrace();
        }
    }
    /**
     * 此静态方法用于连接oracle数据库
     * @return Connection
     * @throws Exception
     */
    public static Connection getOracleConnection() throws Exception {
        try {
            return ds.getConnection();          
        } catch (Exception e) {
            System.out.println("Oracle数据库连接失败!");
            throw e;
        }
    }
    /**
     * 此静态方法用于连接mysql数据库
     * @return Connection
     * @throws Exception
     */
    public static Connection getMysqlConnection() throws Exception {
        try {
            return ds1.getConnection();
        }catch(Exception e) {
            System.out.println("Mysql数据库连接失败!");
            throw e;
        }       
    }
}

以下是测试类,用于向数据库中增删改查的
各个版本的连接数据库的类相同,除了要得的sql语句,这里要注意:每中数据库的sql语句有些许不同,像oracle和mysql相比较而言,比如数据库的基本数据类型就不同,oracle:number(3) ,number(3,2)而mysql:int(3),double(3,2)等等不同

package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class jdbcTest {
    public static void main(String[] args) {
        Connection conn=null;
        try {
            conn=JDBCUtil.getMysqlConnection();
            conn.setAutoCommit(false);
            //这个是mysql的语句,如果连接的是oracle的,要写成//oracle所允许的语句即可
            String sql="create table userinfo.userinfos ( "
                    + "id int(8), "
                    + "username varchar(32), "
                    + "password varchar(32), "
                    + "email varchar(32), "
                    + "account double(7,2) )";
            System.out.println(sql);
            Statement state=conn.createStatement();
            state.execute(sql);
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰河家园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值