一,JDBC连接
Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。
二,使用servlet连接数据库
上午学习了servlet之后就想到可以直接在Init()方法里连接数据库,就在csdn上查了一下,在此总结一下学习结果。
创建一个servlet
/**
* Servlet implementation class ConnectionServlet
*/
public class ConnectionServlet extends HttpServlet {
Connection con;
public void init() {
//获取配置的JDBC驱动,数据库地址,登录用户名和密码
String driver = getInitParameter("driver");
String url = getInitParameter("url");
String name = getInitParameter("user");
String pass = getInitParameter("password");
try {
Class.forName(driver);
con=DriverManager.getConnection(url,name,pass);//连接数据库
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public ConnectionServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException,ServletException{
request.setCharacterEncoding("UTF-8");//设置将字符以"UTF-8"编码输出到客户端浏览器
response.setContentType("text/html;charset=UTF-8");//指定对服务器响应进行重新编码的编码
PrintWriter out=response.getWriter();
String bookName=request.getParameter("bookName");
ResultSet rst=getBook(bookName);
out.println("<table border=1><tr><td>图书名</td><td>出版社</td><td>价格</td></tr>");
try {
while(rst.next()) {
out.println("<tr>");
out.println("<td>"+rst.getString("bookname")+"</td>");
out.println("<td>"+rst.getString("publisher")+"</td>");
out.println("<td>"+rst.getString("price")+"</td>");
out.println("</tr>");
}
out.println("</table>");
rst.close();
}catch(Exception e) {
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException,ServletException{
// TODO Auto-generated method stub
//init();
doGet(request, response);
}
public ResultSet getBook(String bookName) {
try {
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select * from books where bookname='"+bookName+"'");
return result;
}catch(Exception e) {
return null;
}
}
}
创建完成之后,要在web.xml文件配置和映射Servlet,可以见我上一篇文章Servlet学习心得
三,jsp连接
第一步 在要连接数据库的jsp文件中先加入以下代码
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
第二步 添加驱动
第三步 即可在jsp文件中进行对数据库的操作了(增删改查)
四,简单程序代码连接
这种方式,可以直接通过简单代码完成连接并操作数据库,但是一般的项目不这么连
代码如下:
package yhl;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
public class ConnectionTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/课程设计?serverTimezone=UTC&useSSL=false";
String user = "root";
String password = "010110";
Connection conn = (Connection)DriverManager.getConnection(url, user, password);
Statement stat = (Statement)((java.sql.Connection) conn).createStatement();
// stat.execute("insert into vcd values(3,100.0,600.0,'可租','Iron Man',120)");
stat.execute("delete from VCD where Vno=1");
ResultSet rq = stat.executeQuery("select * from vcd");
while(rq.next()) {
System.out.println(rq.getString(1));
System.out.println("光盘号:"+rq.getString("VNo")+" "+"光盘名:"+rq.getString("VName"));
}
if(rq!=null) {
rq.close();
}
if(stat!=null) {
stat.close();
}
if(conn!=null) {
conn.close();
}
}
}
因为最近在做数据库的课程设计,第一次认真学习整个项目的架构,学习连接数据库的第一篇博客
借鉴