Home > Java > javaTutorial > body text

Java realizes a simple example of connecting to oracle/mysql database function

黄舟
Release: 2017-10-14 09:42:11
Original
2215 people have browsed it

This article mainly introduces the function of connecting Oracle/mysql database implemented by java, and analyzes the related operation skills of java based on jdbc to connect Oracle and mysql in the form of examples. It also comes with complete example code and oracle+mysql database driver package for readers. Download for reference, friends in need can refer to

The example in this article describes the function of connecting to the oracle mysql database implemented in java. Share it with everyone for your reference, the details are as follows:


package com.nuo.test.Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil{
    public static Connection getConnection() throws Exception{
        Connection conn=null;
        try {
            Class.forName(
                //"oracle.jdbc.driver.OracleDriver"
                "com.mysql.jdbc.Driver"
            );
            conn=DriverManager.getConnection(
                //"jdbc:oracle:thin:@127.0.0.1:1521:qiye","jossinfo","tao"
                "jdbc:mysql://localhost:3306/care","root","nuo"
            );
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return conn;
    }
    public static void close(Connection conn) throws Exception{
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }
    }
    public static void main(String[] args) throws Exception{
        ResultSet rs = null;
        Connection conn = null;
        String valiresult="";
        try{
            conn = DBUtil.getConnection();
            PreparedStatement prep = conn.prepareStatement(    
                "select * from tab_user "
            );
            rs = prep.executeQuery();
            while(rs.next()){
                valiresult=rs.getString(1);//表第一列内容
                System.out.println(valiresult);
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }finally{
            DBUtil.close(conn);
        }
    }
}
Copy after login

The above is the detailed content of Java realizes a simple example of connecting to oracle/mysql database function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template