一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java实现的连接oracle/mysql数据库功能简单示例【附oracle+mysql数据库驱动包】

时间:2022-06-29 01:16:18 编辑:袖梨 来源:一聚教程网

本文实例讲述了java实现的连接oracle mysql数据库功能。分享给大家供大家参考,具体如下:

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);
        }
    }
}

热门栏目