问答详情
源自:8-3 DBHelper类设计

我用的是Oreclo数据库该怎样写这一段??

private static final String driver = "com.mysql.jdbc.Driver"; //数据库驱动
    //连接数据库的URL地址    
    private static final String url="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8";
    private static final String username="root";//数据库的用户名
    private static final String password="";//数据库的密码

提问者:慕用7772223 2016-11-13 14:41

个回答

  • 超速_蜗牛
    2016-12-12 17:17:44

    package util;


    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.SQLException;


    public class DBHelper {

    private static Connection connection=null;

    private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";

    private static final String URL = "jdbc:oracle:thin:@localhost:1521:orc";

    private static final String USERNAME = "SYS as SYSDBA";

    private static final String PASSWORD = "admin";

    static{

    try {

    Class.forName(DRIVER);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    private static Connection getConnection() {

    if (connection==null) {

    try {

    connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    return connection;

    } else {

    return connection;

    }

    }

    private static void closeConnection(Connection connection) {

    if (connection!=null) {

    try {

    connection.close();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    public static void main(String[] args) {

    Connection conn = null;

    conn = DBHelper.getConnection();

    if (conn!=null) {

    System.out.println("数据库连接正常");

    }else {

    System.out.println("数据库连接异常");

    }

    DBHelper.closeConnection(conn);

    }

    }


  • GoingForward
    2016-11-14 11:58:58

    应该是其他步骤都一样,改一下数据库驱动和url地址就行了;

    可以参考博客园的一篇文章:http://www.cnblogs.com/liuxianan/archive/2012/08/05/2624300.html

    private static final String driver = "oracle.jdbc.driver.OracleDriver";//数据库驱动

    private static final String url = "jdbc:oracle:thin:@localhost:1521:shopping";//连接数据库的URL地址 

    private static final String username="root";//数据库的用户名
    private static final String password="";//数据库的密码

    oracle里的jdbc url没有设定字符集的参数,下面是CSDN上一个讨论帖,个人觉得里面那个while (rs.next()) { 
    ARRAY my_array = ((OracleResultSet)rs).getARRAY(1); 。。。}的示例不错
    :

    http://blog.csdn.net/haiross/article/details/38380449