JDBC的前世今生
“请必须要有自信,你就是一道风景,没必要在别人风景里面仰视。”你好,我是梦阳辰,快来和我一起学习吧!
概念:Java DataBase Connectivity ,java数据库连接,Java语言操作数据库。
JDBC本质:sun公司定义的一套操作所有关系型数据库的接口。各个数据库厂商去实现这套接口(提供数据库驱动jar包)。我们可以使用这套接口(jdbc)编程,真正执行的代码时驱动jar包中的实现类。但我们只需要面向接口编程。

步骤:
1.下载驱动:去你使用的数据库厂商的官网找到你要使用的数据库版本对应的版本驱动,下载驱动。
2.将驱动jar包导入项目
1.赋值mysql-connector-java-5.1.xx-bin.jar到项目的lib目录下。
然后单击目录右键–>Add as Library。
3.注册驱动(告诉程序你用的是哪个厂商,哪个版本的数据库)
4.获取数据库连接对象(Connetion)(表示JVM的进程和数据库进程之间的通道打开了,这属于进程之间的通信,重量级的,使用完之后一定要关闭)
5.定义sql
6.获取执行sql语句的对象(Statement)(专门执行sql语句的对象)
7.执行sql,接受返回结果
8.处理结果
9.释放连接
注意:用文本编辑器开发,需要配置环境变量到classpath中。
如果使用IDEA工具的时候,不需要配置classpath环境变量,按要求导入jar包到项目即可。
@WebServlet("/com/teaching/case631/JDBC")
public class JDBC extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JDBC() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Connection conn =null;
Statement stat = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.设置连接参数,url,user,password
String url = "jdbc:mysql://localhost:3306/teaching";
String user ="root";
String password ="xxxx";
String useUnicode = "true";//设置编码
String characterEncoding = "UTF8";
/*url="jdbc:mysql://localhost:3306/teaching"
+"?user=root"
+"&password="xxxxx"
+"&userUnicode=true"
+"&characterEncoding=UTF8";
*/
//conn = DriverManager.getConnection(url);
//3.获取数据库连接
conn = DriverManager.getConnection(url,user,password);
stat = conn.createStatement();
//5.定义sql语句
String sql = "update student set name = '张三' where name='梦阳辰'";
//6.利用载体执行SQL语句
int count = stat.executeUpdate(sql);
//7.处理返回结果
if(count>0) {
out.print("连接数据库成功!");
}else {
out.print("连接数据库失败!");
}
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//7.关闭连接
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( conn !=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
查询:
@WebServlet("/com/teaching/case631/JDBC")
public class JDBC extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JDBC() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Connection conn =null;
Statement stat = null;
ResultSet rs = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.设置连接参数,url,user,password
String url = "jdbc:mysql://localhost:3306/teaching";
String user ="root";
String password ="0910";
String useUnicode = "true";
String characterEncoding = "UTF8";
//3.获取数据库连接
conn = DriverManager.getConnection(url,user,password);
stat = conn.createStatement();
//5.定义sql语句
String sql = "select * from student";
//6.利用载体执行SQL语句
rs = stat.executeQuery(sql);
//7.处理返回结果
while(rs.next()){
int id = rs.getInt("id");
String sequence = rs.getString("sequence");
String name = rs.getString("name");
String sex = rs.getString("sex");
Date birthday = rs.getDate("birthday");
out.println(id+" "+sequence+" " +name+" "+sex+" "+birthday);
}
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//7.关闭连接
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(
