jdbc----->java data base connectivity(数据库连接)
--通过接口来取得连接驱动,api(java.sql;javax.sql)mysql-driver:(MySql-connector-java-version-bin.jar)
简单事例:
create database test Character set utf8collate utf8_general_ci;
use test
create table user
(
id int primary Key,
name varchar(20),
password varchar(20),
email varchar(20),
birthday Date
);
insert into user(id,name,password,email,birthday) values(1,'a','a1','a@sina.com','1900-1-1');
junitTest---->
public class junitTest{
public static void main(String[] args) throws Exception{
String url="jdbc:mysql://localhost:3306/test_jdbc";
String usename="root";
String password="123456";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url,username,password);
Statement stm=conn.createStatement();
String sql="select id,name,password,email,birthday from user";
ResultSet rs=stm.executeQuery(sql);
while(rs.next()){
int id=(Integer)rs.getObject("id");
String name=(String)rs.getObject("name");
String password=(String)rs.getObject("password");
......
}
finally{
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
rs=null;
}
if(stm!=null){
try{
stm.close();
}catch(Exception e){
e.printStackTrace();
}
stm=null;
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
conn=null;
}
JDBC 常用API---->
createStatement();
prepareStatement(sql);
prepareCall(sql);
setAutoCommit();
callbakc();
commit();
Statement常用方法-->
executeQuery(sql)
executeUpdate(sql)
execute();
addBatch();
executeBatch();
ResultSet使用方法-->
rs.getObject(String columnName/int index);
rs.getString(String columnName/int index);
rs.next()-->true/false;
包装JDBC---->JDBCUtils
core-code:
private static String driver=null;
private static String url=null;
private static String username=null;
private static String password=null;
InputStream is=JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Property pro=new Property();
pro.load(is);
driver=pro.getProperty("driver");
url=pro.getProperty("url");
username=pro.getProperty("username");
password=pro.getProperty("password");
Class.forName(driver);
public static Connection getConn() throws Exception{
return DriverManager.getConnection(url,username,password);
}
public static void releaseResources(Connection conn,Statement st,ResultSet rs){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
rs=null;
}...
db.properties--->driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcUtils
username=root
password=123456
使用JDBCUtils实现CRUD例子数据库定义语言(DDL-->insert,update,delete DML(databas 操作语言)-->query)
public void testCreate() throws Exception{
Connection conn=JDBCUtils.getConn();
Statement st=conn.createStatement();
insertMethod-->
PrepareStatmen pst=conn.prepareStatement(sql);
String sql="insert into user(id,name,email) values(2,'sand','sand@qq.com')";
int rsCount=st.executeUpdate(sql);
if(rsCount>0){
System.out.println("insert success");
}else{
System.out.println("occur error");
}
deleteMethod-->
String sql="delete from user where id=2";
st.executeUpdate(sql);...
updateMehod-->
String sql="update use set name='king' where id=1";
st.executeUpdate(sql);
queryMehod-->
String sql=''select id,name,email from user";
ResutSet rs=st.executeQuery(sql);
while(rs.next()){
int id=(Integer)rs.getObject("id");....
}