JDBC连接数据库1

基础使用

properities 属性设置

url=jdbc:oracle:thin:@127.0.0.1:1521:XE
username=scott
passwd=tiger

简版JDBC连接数据库,使用DML

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明确我们要干什么?
        //  1.数据库管理
        //  2.数据库连接
        //  3.建立执行平台
        //  4.执行SQL语句
        //  5.平台关闭
        
        //1.
        Class.forName("oracle.jdbc.driver.OracleDriver");
                //2.
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "scott", "tiger");
                //3.
        Statement st = (Statement) conn.createStatement();
        String sql ="insert into dept values (88,'NetWork','BOSTON')";
                //4.
        st.executeUpdate(sql);
                //5.
        st.close();
        conn.close();

}

简版JDBC连接数据库,使用Select

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明确我们要干什么?
        //  1.数据库管理
        //  2.数据库连接
        //  3.建立执行平台
        //  4.执行SQL语句
        //  5.平台关闭

        //1.
        Class.forName("oracle.jdbc.driver.OracleDriver");
                //2.
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "scott", "tiger");
                //3.
        Statement st = (Statement) conn.createStatement();
        String sql ="select * from dept";
        st.executeQuery(sql);
                //4.
        ResultSet resultSet = st.getResultSet();
        while (resultSet.next()) {
            String num = resultSet.getString(1);
            String work = resultSet.getString(2);
            String loc = resultSet.getString("loc");
            System.out.println(num+","+work+","+loc);
        }
        resultSet.close();      
        st.close();
        conn.close();

}

简版JDBC连接数据库,无冲突版使用Select

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明确我们要干什么?
        //  1.数据库管理
        //  2.数据库连接
        //  3.建立执行平台
        //  4.执行SQL语句
        //  5.平台关闭


        //1.
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2.
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "scott", "tiger");
        //3.
        Statement st = (Statement) conn.createStatement();
        String sql ="select * from emp";
        st.executeQuery(sql);
        //4.
        ResultSet resultSet = st.getResultSet();
        ResultSetMetaData date = resultSet.getMetaData();
        int count = date.getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= count; i++) {
                String string = date.getColumnName(i);
                String value = resultSet.getString(i);
                System.out.print(string+","+value+"  ");
            }
            System.out.println();
        }
        //5.
        resultSet.close();      
        st.close();
        conn.close();
    
}

简版JDBC连接数据库,进行项目分级,配置文件路径(properties)

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明确我们要干什么?
        //  1.找路径
        //  2.取得属性
        //  3.数据库管理
        //  4.数据库连接
        //  5.建立执行平台
        //  6.执行SQL语句
        //  7.平台关闭

        //1.
        File file =new File(".");
        System.out.print(file.getAbsolutePath());
        //2.
        Properties properties = new Properties();
        FileInputStream inputStream = new FileInputStream("./src/db.properties");
        properties.load(inputStream);
        inputStream.close();
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String passwd = properties.getProperty("passwd");
        //3.
        Connection conn = DriverManager.getConnection(url, username, passwd);
        //4.
        Statement st = (Statement) conn.createStatement();
        //5.
        String sql ="select * from emp";
        st.executeQuery(sql);
        //6.
        ResultSet resultSet = st.getResultSet();
        ResultSetMetaData date = resultSet.getMetaData();
        int count = date.getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= count; i++) {
                String string = date.getColumnName(i);
                String value = resultSet.getString(i);
                System.out.print(string+","+value+"  ");
            }
            System.out.println();
        }
        //7.
        resultSet.close();      
        st.close();
        conn.close();
    
}

你可能感兴趣的:(JDBC连接数据库1)