1.JDBC的概念
JDBC(Java Database Connectivity)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC提供了对Java程序员,数据库厂商、第三方中间件厂商的API JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据
2.使用步骤
(1)加载驱动程序
Class
.forName("com.mysql.jdbc.Driver");
Class
.forName("oracle.jdbc.driver.OracleDriver");
(2)获得数据库连接
URL的格式:“协议:子协议://主机:端口号/数据库名”
例如:“jdbc:mysql://127.0.0.1:3306/imooc”
Connection conn
= DriverManager
.getConnection(URL
, USER
, PASSWORD
);
(3)创建Statement、PreparedStatement对象
1.创建Statement对象 Statement stmt = conn.createStatement(); 1.创建PreparedStatement对象 PreparedStatement ptmt = conn.prepareStatement(sql);
常用的Statement方法
execute(String sql):运行语句,返回是否有结果集executeQuery(String sql):运行select语句,返回ResultSet结果集。executeUpdate(String sql):运行insert/update/delete操作,返回更新行数。addBatch(String sql) :把多条sql语句放到一个批处理中executeBatch():向数据库发送一批sql语句执行。
conn
.createStatement();
conn
.prepareStatement(sql
);
Statement、prepareStatement区别和联系
prepareStatement中 sql语句中会有问号,即为不确定的操作数据,在下面的代码中,执行一天插入语句,中间的问号即为不确定的插入数,在预执行时,仅仅只是知道接下来需要插入数据这一操作,而不知道具体的插入数据,如果需要插入多条数据,仅仅改变问号的值即可,计算机不会再判断是什么操作,因为在预执行时它就知道是插入语句了。
Class
.forName(driver
);
conn
= DriverManager
.getConnection(url
, userName
, passWord
);
String sql
="insert into student values(?,?,?)";
PreparedStatement ps
=conn
.prepareStatement(sql
);
ps
.setInt(1,20);
ps
.setString(2,"miaoling");
java
.util
.Date utilDate
=new java.util.Date();
ps
.setDate(3, new java.sql.Date(utilDate
.getTime()));
System
.out
.println(ps
.execute());
Statement中sql语句就是完整的数据库插入语句,对于一条数据而言没有什么性能问题,但是数据量一旦很多,就会在每次插入数据的时候执行完整的数据库语句,计算机每次都要判断是什么操作的语句再执行该操作,性能就会大大降低。
Class
.forName(driver
);
conn
= DriverManager
.getConnection(url
, userName
, passWord
);
stat
=conn
.createStatement();
String sql
="insert into student values(20,'miaoling',to_date('21-9-2016','dd-mm-yyyy'))";
System
.out
.println(stat
.execute(sql
));
注意!!!
在Statement、prepareStatement最后执行时,前者是stat.execute(sql),后者是ps.execute()
增删改查完整实例演示
public class DbUtil {
public static final String URL
= "jdbc:mysql://localhost:3306/imooc";
public static final String USER
= "liulx";
public static final String PASSWORD
= "123456";
private static Connection conn
= null
;
static{
try {
Class
.forName("com.mysql.jdbc.Driver");
conn
= DriverManager
.getConnection(URL
, USER
, PASSWORD
);
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
public static Connection
getConnection(){
return conn
;
}
}
package liulx
.model
;
import java
.util
.Date
;
public class Goddess {
private Integer id
;
private String user_name
;
private Integer sex
;
private Integer age
;
private Date birthday
;
private String email
;
private String mobile
;
private String create_user
;
private String update_user
;
private Date create_date
;
private Date update_date
;
private Integer isDel
;
}
package liulx
.dao
;
import liulx
.db
.DbUtil
;
import liulx
.model
.Goddess
;
import java
.sql
.Connection
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;
import java
.util
.ArrayList
;
import java
.util
.List
;
public class GoddessDao {
public void addGoddess(Goddess g
) throws SQLException
{
Connection conn
= DbUtil
.getConnection();
String sql
= "INSERT INTO imooc_goddess(user_name, sex, age, birthday, email, mobile,"+
"create_user, create_date, update_user, update_date, isdel)"
+"values("+"?,?,?,?,?,?,?,CURRENT_DATE(),?,CURRENT_DATE(),?)";
PreparedStatement ptmt
= conn
.prepareStatement(sql
);
ptmt
.setString(1, g
.getUser_name());
ptmt
.setInt(2, g
.getSex());
ptmt
.setInt(3, g
.getAge());
ptmt
.setDate(4, new Date(g
.getBirthday().getTime()));
ptmt
.setString(5, g
.getEmail());
ptmt
.setString(6, g
.getMobile());
ptmt
.setString(7, g
.getCreate_user());
ptmt
.setString(8, g
.getUpdate_user());
ptmt
.setInt(9, g
.getIsDel());
ptmt
.execute();
}
public void updateGoddess(){
Connection conn
= DbUtil
.getConnection();
String sql
= "UPDATE imooc_goddess" +
" set user_name=?, sex=?, age=?, birthday=?, email=?, mobile=?,"+
" update_user=?, update_date=CURRENT_DATE(), isdel=? "+
" where id=?";
PreparedStatement ptmt
= conn
.prepareStatement(sql
);
ptmt
.setString(1, g
.getUser_name());
ptmt
.setInt(2, g
.getSex());
ptmt
.setInt(3, g
.getAge());
ptmt
.setDate(4, new Date(g
.getBirthday().getTime()));
ptmt
.setString(5, g
.getEmail());
ptmt
.setString(6, g
.getMobile());
ptmt
.setString(7, g
.getUpdate_user());
ptmt
.setInt(8, g
.getIsDel());
ptmt
.setInt(9, g
.getId());
ptmt
.execute();
}
public void delGoddess(){
Connection conn
= DbUtil
.getConnection();
String sql
= "delete from imooc_goddess where id=?";
PreparedStatement ptmt
= conn
.prepareStatement(sql
);
ptmt
.setInt(1, id
);
ptmt
.execute();
}
public List
<Goddess> query() throws SQLException
{
Connection conn
= DbUtil
.getConnection();
Statement stmt
= conn
.createStatement();
ResultSet rs
= stmt
.executeQuery("SELECT user_name, age FROM imooc_goddess");
List
<Goddess> gs
= new ArrayList<Goddess>();
Goddess g
= null
;
while(rs
.next()){
g
= new Goddess();
g
.setUser_name(rs
.getString("user_name"));
g
.setAge(rs
.getInt("age"));
gs
.add(g
);
}
return gs
;
}
public Goddess
get(){
Goddess g
= null
;
Connection conn
= DbUtil
.getConnection();
String sql
= "select * from imooc_goddess where id=?";
PreparedStatement ptmt
= conn
.prepareStatement(sql
);
ptmt
.setInt(1, id
);
ResultSet rs
= ptmt
.executeQuery();
while(rs
.next()){
g
= new Goddess();
g
.setId(rs
.getInt("id"));
g
.setUser_name(rs
.getString("user_name"));
g
.setAge(rs
.getInt("age"));
g
.setSex(rs
.getInt("sex"));
g
.setBirthday(rs
.getDate("birthday"));
g
.setEmail(rs
.getString("email"));
g
.setMobile(rs
.getString("mobile"));
g
.setCreate_date(rs
.getDate("create_date"));
g
.setCreate_user(rs
.getString("create_user"));
g
.setUpdate_date(rs
.getDate("update_date"));
g
.setUpdate_user(rs
.getString("update_user"));
g
.setIsDel(rs
.getInt("isdel"));
}
return g
;
}
}