1.现在虽然很少手动写jdbc来操作数据库,大部分都是通过框架来映射了,但是有时候碰到多数据源的时候,配置起来很麻烦,这个时候jdbc就有很好的作用了。
2.不多说,直接上代码,工具类,下面会有demo来使用这个工具类
a.不要忘了导入相应数据库连接的Jar包
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author yes
* 2016.11.2
*/
public class JdbcTool {
private String url;
private String name;
private String pwd;
private String driverName = "oracle.jdbc.OracleDriver";// 默认为oracle的驱动
//private String driverName = "com.mysql.jdbc.Driver";//MySql驱动
//构造方法
public JdbcTool() {
super();
}
//方法重载,三个参数
public JdbcTool(String url, String name, String pwd) {
super();
this.url = url;
this.name = name;
this.pwd = pwd;
}
//方法重载,四个参数
public JdbcTool(String url, String name, String pwd, String driverName) {
super();
this.url = url;
this.name = name;
this.pwd = pwd;
this.driverName = driverName;
}
//参数设置set和get方法,手动设置参数
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
//连接数据库
public Connection getConnection() throws SQLException {
Connection conn = null;
try {
Class.forName(this.driverName);// 加载驱动类
String url = this.url;//连接的地址
String user = this.name;// 用户名
String password = this.pwd;// 登录密码
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return conn;
}
//关闭连接,释放资源
public void closeConn(ResultSet result,
PreparedStatement ps, Connection conn) {
// 关闭结果集
if (result != null) {
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭预编译对象
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
工具类里面注解很详细,下面看怎样调用工具类。
3.demo调用工具类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* @author yes
* 2016.11.2
*/
public class TestConnect {
//private static JdbcTool jdbcTool = new JdbcTool();// 初始化工具类
//直接赋值,调用重载的构造方法对参数赋值
private static JdbcTool jdbcTool = new JdbcTool("url","pwd","name");
public static void main(){
//在初始化工具类的基础上,使用set方法赋值
/*jdbcTool.setUrl("jdbc:oracle:thin:@127.0.0.1:1521/orcl");
jdbcTool.setPwd("itsm_springmvc");
jdbcTool.setName("itsm_springmvc");*/
//初始化三个对象
Connection conn = null;//连接对象
PreparedStatement ps = null;//预处理对象
ResultSet result = null;//结果集
try {
// 实例化connection对象
conn = jdbcTool.getConnection();
String sql = "select * from table where password = ? and name = ?";//sql语句
//使用PreparedStatement可以进行预处理
ps = conn.prepareStatement(sql);
ps.setString(1,"");
ps.setString(2,"");
//执行sql,执行不同语句返回结果集类型不同,如果是插入或者更新,返回的result是int类型的
result = ps.executeQuery();
//取出结果集中的数据,放到map中,最后可添加到list中,看需求
while (result.next()) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", result.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源,方法在工具类中
jdbcTool.closeConn(result, ps, conn);
}
}
}
4.上面有两种方法,一种是初始化工具类后,使用类中的set方法给参数赋值;另一种是直接初始化的时候赋值,调用里面的重载的构造方法。