一:jdbc
sun:提供了一套通用性的接口:可以连接任何的数据库:
连接数据库的具体得到实例,具体的数据库厂商实现的。
连接数据的步骤(别忘了复制jar包):(
1)注册驱动: class.forname():drivermanager
(2)获得链接对象:connection
(3)创建sql容器:语句:
(4)执行sql语句:: stmt
(5)查询操作:遍历结果集:resultset
(6)关闭资源:
resultset:详解(更多可以看api)
封装了结果集的对象:内部有一个可移动的光标,默认情况,指向第一条记录集的上一条记录:
next();光标下移动一次:返回的boolean的值;判断是否有结果可以被遍历:
previous();光标上移动一次:
last()移动到最后一行:
afterlast();移动到最后一行之后:
beforefirst()移动到第一行的之前:
first()
getobject();
getint();
的getstring();
getfloat();
getdouble()
getdate();
getxxx()
的的getmetadata()获得结果集的元数据:(重要)
package com.yidongxueyuan.dao;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
public class studentdaoimpl {
public static void main(string[] args) {
try {
class.forname("com.mysql.jdbc.driver");
connection conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/jdbc01", "root", "root");
statement stmt = conn.createstatement();
string sql ="select * from student ";
resultset rs = stmt.executequery(sql);
/*while(rs.next()){
int id = rs.getint("sid");
string name = rs.getstring("sname");
system.out.println(id " " name);
}
*/
rs.next();
//上移动一次:
// rs.previous();
// rs.last();
// rs.first() ;
rs.beforefirst();
while(rs.next()){
// object object = rs.getobject("1");
int id = rs.getint("sid");
string name = rs.getstring("sname");
system.out.println(id " " name);
}
rs.close();
stmt.close();
conn.close();
} catch (classnotfoundexception e) {
e.printstacktrace();
} catch (sqlexception e) {
e.printstacktrace();
} finally{
}
}
}
// mysql封装:工具类:
封装好的util:
package com.yidongxueyuan.dao;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
/*
* 对jdbc 进行封装:
*/
public class jdbcutils2 {
//准备数据库的四大参数:
private static final string driver ="com.mysql.jdbc.driver";
private static final string url ="jdbc:mysql://localhost:3306/jdbc01";
private static final string username ="root";
private static final string password ="root";
/*
* 驱动: 只需要注册一次就ok
*
*/
static{
try {
class.forname(driver);
} catch (classnotfoundexception e) {
e.printstacktrace();
}
}
/*
* 1:创建工具类: 直接获得一个连接对象:
*/
public static connection getconnection (){
try {
connection connection = drivermanager.getconnection(url, username, password);
return connection;
} catch (sqlexception e) {
throw new runtimeexception(e);
}
}
/*
* 定义一个方法: 释放资源: 直接将rs stmt conn 全部释放:
*/
public static void release(resultset rs ,statement stmt , connection conn){
if(rs!=null){
try {
rs.close();
} catch (sqlexception e) {
e.printstacktrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (sqlexception e) {
e.printstacktrace();
}
stmt=null;
}
if(conn!=null){
try {
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
conn=null;
}
}
}
调用工具类实现操作:
package com.yidongxueyuan.dao;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
/*
* 使用工具类: 对代码进行重构:
*/
public class studentdaoimpl2 {
public static void main(string[] args) {
connection conn=null;
statement stmt=null;
resultset rs=null;
try {
//使用工具类 获得一个连接对象:
conn = jdbcutils2.getconnection();
stmt = conn.createstatement();
string sql ="select * from student ";
rs = stmt.executequery(sql);
while(rs.next()){
// object object = rs.getobject("1");
int id = rs.getint("sid");
string name = rs.getstring("sname");
system.out.println(id " " name);
}
} catch (sqlexception e) {
e.printstacktrace();
} finally{
jdbcutils2.release(rs, stmt, conn);
}
}
}
上面的代码是硬编码,如果是软编码的话步骤如下:
1个方言文件内容:
# this is my mysql configuration
driver =com.mysql.jdbc.driver
url =jdbc:mysql://localhost:3306/jdbc01
username =root
password =root
2 jdbcutil.java读取如下:
package com.yidongxueyuan.dao;
import java.io.ioexception;
import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.properties;
/*
* 读取外部的配置文件
*/
public class jdbcutil {
private static final properties p= new properties();
private static string url;
private static string driver;
private static string username;
private static string password;
static{
try {
//将外部的配置文件进行读取: 类的加载器:
inputstream in = jdbcutil.class.getclassloader().getresourceasstream("dbconfig.properties");
p.load(in);
driver = p.getproperty("driver");
class.forname(driver);
} catch (ioexception e) {
e.printstacktrace();
} catch (classnotfoundexception e) {
e.printstacktrace();
}
}
/*
* 1:创建工具类: 直接获得一个连接对象:
*/
public static connection getconnection (){
try {
url = p.getproperty("url");
username = p.getproperty("username");
password = p.getproperty("password");//
connection connection = drivermanager.getconnection(url, username, password);
return connection;
} catch (sqlexception e) {
throw new runtimeexception(e);
}
}
//释放连接:
/*
* 定义一个方法: 释放资源: 直接将rs stmt conn 全部释放:
*/
public static void release(resultset rs ,statement stmt , connection conn){
if(rs!=null){
try {
rs.close();
} catch (sqlexception e) {
e.printstacktrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (sqlexception e) {
e.printstacktrace();
}
stmt=null;
}
if(conn!=null){
try {
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
conn=null;
}
}
/*public static void main(string[] args) {
connection connection = getconnection();
system.out.println(connection);
}*/
}
使用上面的工具类完成增加改查操作:
package com.yidongxueyuan.dao2;
import java.sql.connection;
import java.sql.statement;
import java.util.date;
import org.junit.test;
import com.yidongxueyuan.dao.jdbcutil;
public class studentcrud {
//insert 插入:
@test
public void insertstudent() throws exception {
connection conn = jdbcutil.getconnection();
statement stmt = conn.createstatement();
int num= stmt.executeupdate("insert into student (sname, birthday) "
"values('xxx','" new java.sql.date(system.currenttimemillis()) "')");
if(num>0){
system.out.println("插入成功");
}else{
system.out.println("插入失败");
}
jdbcutil.release(null, stmt, conn);
}
//删除:
@test
public void deletestudent() throws exception {
connection conn = jdbcutil.getconnection();
statement stmt = conn.createstatement();
string sql="delete from student where sid=2 ";
int num= stmt.executeupdate(sql);
system.out.println("num:" num);
if(num>0){
system.out.println("删除成功");
}else{
system.out.println("删除失败");
}
jdbcutil.release(null, stmt, conn);
}
//修改:
@test
public void updatestudent() throws exception {
connection conn = jdbcutil.getconnection();
statement stmt = conn.createstatement();
string sql="update student set sname='yyyyy' where sid =3 ";
int num= stmt.executeupdate(sql);
system.out.println("num:" num);
if(num>0){
system.out.println("修改成功");
}else{
system.out.println("修改失败");
}
jdbcutil.release(null, stmt, conn);
}
//查:
}