标签:generate ace cti connect sys sql数据库 oracle except dbutil
1.连接MySQL数据库
public class MySQLDBUtil {
//创建驱动
public static final String USER="root";//数据库账号
public static final String PWD="123456";//密码
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306/shoppingcat";//要连接的数据库
static PreparedStatement pst = null;
static Connection con = null;
static ResultSet rest = null;
//编写连接MySQL数据库的方法
//这里使用静态方法,不用创建对象也可以直接调用
//这里Connection 是java编写的用于连接数据库的类(对象)
public static Connection getconnection() {
Connection con =null;
//定义一个新的connection对象命名为con,赋值null
try {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con=DriverManager.getConnection(URL, USER, PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
2.连接Oracle数据库
public class OrcaleDBUtil {
public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
public static final String USER = "dan_1";//数据库名称
public static final String PWD = "1234567";//密码
public static Connection getOracleConnection(){
Connection OracleCon = null;
try {
Class.forName(DRIVER);
OracleCon = DriverManager.getConnection(URL, USER, PWD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return OracleCon;
}
public static void main(String[] args) {
System.out.println(getOracleConnection());
}
}
标签:generate ace cti connect sys sql数据库 oracle except dbutil
原文地址:http://www.cnblogs.com/Sscholar/p/7498767.html