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

来源:互联网 发布:淘宝详情页上传步骤 编辑:程序博客网 时间:2024/05/21 17:56
以下是写在config.properties文件里面的内容(除了加的中文注释):OracleDriverName=oracle.jdbc.driver.OracleDriverOracleUrl=jdbc:oracle:thin:@127.0.0.1:1521:xe    xe为oracle数据库的SID,每个人的都不一样,要修改成自己的OracleUserName=OraclePassword=MysqlDriverName=com.mysql.jdbc.DriverMysqlUrl=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();                }            }        }    }}