保捱科技网
您的当前位置:首页java 使用 JDBC 连接数据库

java 使用 JDBC 连接数据库

来源:保捱科技网

读取sql文件                                                                                                                                                                                                                                                           

读取已经写好的sql文件使用:@+具体路径即可!!!

连接Oracle                                                                                                                                                                                                                                                            

@Test
    public void Test_jdbc_1() throws SQLException{
        Driver driver=new oracle.jdbc.driver.OracleDriver();    //连接oracle
        
        String url="jdbc:oracle:thin:@10.0.21.199:1521:wuyu";      //url的格式一定要正确
        Properties info=new Properties();
        info.put("user", "scott");             //user         key是不能更改的
        info.put("password", "1230");        //password     key也是不能更改的
        
        Connection connection = driver.connect(url, info);
        System.out.println(connection);
        }

连接MySql                                                                                                                                                                                                                                                             

public void Test_jdbc_2() throws SQLException {
        //1. 创建一个 Driver 实现类的对象
        Driver driver = new com.mysql.jdbc.Driver(); //连接mysql
    
        //2. 准备连接数据库的基本信息: url, user, password
        String url = "jdbc:mysql://localhost:3306/test";           //url的格式一定要正确
        Properties info = new Properties();
        info.put("user", "root");
        info.put("password", "1230");
        
        //3. 调用 Driver 接口的 connect(url, info) 获取数据库连接
        Connection connection = driver.connect(url, info);
//        System.out.println(connection);
    }


oracle 的jar 包可以在 orcle上找到;路径是:D:\oracle\product\10.2.0\db_1\jdbc\lib

mysql 的我也不知道。。。哈哈哈/



转载于:https://my.oschina.net/Early20/blog/484826

因篇幅问题不能全部显示,请点此查看更多更全内容