我以前用到的,oracle数据库的:
创新互联建站致力于互联网网站建设与网站营销,提供网站制作、网站建设、网站开发、seo优化、网站排名、互联网营销、小程序设计、公众号商城、等建站开发,创新互联建站网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。
package com.icool点抗 mon.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author ZH_Q
* @version 1.0
*/
public class GetConn {
private Connection conn=null;
private String usName;
private String usPwd;
private String Clfn;
private String dmName;
//调用空参构造,默认启用本地数据库
public GetConn() {
this.Clfn ="oracle.jdbc.driver.OracleDriver";
this.dmName ="jdbc:oracle:thin:@localhost:1521:orcl";
this.usPwd = "q792002998";
this.usName = "system";
}
/**
* @return 数据库连接对象
*/
public Connection getConn() {
try
{
Class.forName(Clfn);
conn = DriverManager.getConnection(dmName,usName,usPwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* @return 根据SQL语句查询出的结果集
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException {
conn =getConn();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
/**
* @return 影响数据行数
* @throws SQLException
*/
public int executeUpdate(String sql) throws SQLException {
Statement stmt = null;
int i = 0;
getConn();
stmt = conn.createStatement();
i = stmt.executeUpdate(sql);
return i;
}
/**
* @return 根据SQL语句返回预编译对象
* @throws SQLException
*/
public PreparedStatement PreparedStatement(String sql) throws SQLException {
PreparedStatement pstmt = null;
getConn();
pstmt= conn.prepareStatement(sql);
return pstmt;
}
/**
* @param 关闭数据库连接
* @throws DataBaseExpection
*/
public void close(){
if(conn!=null) {
try
{
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* @param 设置是否自动提交
* @throws SQLException
*/
public void setAutoCommit(boolean b) throws SQLException {
getConn();
conn.setAutoCommit(b);
}
public void commit() throws SQLException {
getConn();
conn点抗 mit();
}
public void rollback() throws SQLException {
getConn();
conn.rollback();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private final String DRIVER="com.mysql.jdbc.Driver";//mysql数据库驱动
private final String URL="jdbc:mysql://localhost:3306/test";//url
private final String USERNAME="root";//用户名
private final String PASSWORD="root";
Connection conn=null;//定义对象
public DBConnection(){//初始化
try{
Class.forName(DRIVER);
conn =DriverManager.getConnection(URL,USERNAME,PASSWORD);//
System.out.println("连接成功!");
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
public Connection getConnection(){
return this.conn;
}
public void distory(){//用完数据库关闭
try{
conn.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
}
用的时候直接调用conn这个变量就可以了!!!!
public Connection getConnection(){//事先导入驱动
try{
Class.forname("sun.jdbc.odbc.jdbcodbcDriver");//加载驱动
System.out.println("驱动加载完毕");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try{
Connecion con=DriverManager.getConnecion("jdbc:odbc:数据库名成","用户名","密码");//创建连接对象,即已经连接到数据库
system.out.println("连接成功!");
}
catch(exception e){
e.printStackTrace();
}
}