慕用7772223
2016-11-13 14:41
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="";//数据库的密码
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);
}
}
应该是其他步骤都一样,改一下数据库驱动和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); 。。。}的示例不错
:
JAVA遇见HTML——JSP篇
248278 学习 · 3071 问题
相似问题
回答 1
回答 1