jsp与mysql数据库链接,插入等操作

1. 连接数据库的代码
1.1 导入sql包
<% import="java.sql.*" %>

1.2 连接、查询、关闭数据库
怎样查看冰箱里面有没有大象?1、打开冰箱门。2、看一下。3、关闭冰箱门。就是这个过程。。。。。

[html] 
<% 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    String url="jdbc:mysql://localhost:3306/news"; 
    String user="root"; 
    String password="1234"; 
//配置并连接数据库 
    Connection conn = DriverManager.getConnection(url, user, password); 
    Statement st = conn.createStatement(); 
//查询语句,显示最后10条并且倒序排列 
    ResultSet rs = st.executeQuery("SELECT * FROM data ORDER BY id DESC LIMIT 10"); 
//输出表头 
    out.println("<tr><td>标题</td><td>内容</td><td>时间</td></tr>"); 
//依次输出每个查询结果 
    while(rs.next()){ 
    out.print("<tr><td>"+rs.getString("title")+"</td><td>"+rs.getString("content")+"</td><td>"+rs.getString("date")+"</td></tr><br>"); 
//如果采用列名,要加引号 
    } 
    out.print("</table><hr>"); 
//断开数据库 
    conn.close(); 
%> 

2. 页面的HTML代码
为了页面漂亮一点,做点点美化~做一个表格来存放数据

 

[html] 
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gb2312"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<%request.setCharacterEncoding("UTF-8");%>  
<%response.setCharacterEncoding("UTF-8");%> 
<span><span class="tag"></span></span><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<style type="text/css"> 
table{ width:800px; margin:auto; padding: 5px; font-size:12px; border:0px; background:#00CCFF;} 
tr{ background:#fff;} 
td{ padding: 5px;} 
#title{ text-align:center;} 
</style> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>JSP页面中的数据库查询</title> 
</head> 
 
<body> 
 <form method=post> 
    标题:<input type="text" name="title"/><br/> 
    内容:<input type="text" name="content"/><br/> 
    <input type="submit" value="提交" /> 
 </form> 
    <table > 
  <tr> 
    <td width="174" id="title">标题</td> 
    <td width="449" id="title">内容</td> 
    <td width="161" id="title">时间</td> 
  </tr> 
     
     
  <tr> 
    <td width="174" >  </td> 
    <td width="449" >  </td> 
    <td width="161">  </td> 
  </tr> 
 
 </table> 
</body> 
</html> 
3. 把前面两个代码放在一起
放的时候注意代码的位置安排。
[html] 
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gb2312"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<%request.setCharacterEncoding("UTF-8");%>  
<%response.setCharacterEncoding("UTF-8");%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<style type="text/css"> 
table{ width:800px; margin:auto; padding: 5px; font-size:12px; border:0px; background:#00CCFF;} 
tr{ background:#fff;} 
td{ padding: 5px;} 
#title{ text-align:center;} 
</style> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>无标题文档11</title> 
</head> 
 
<body> 
 <% 
 //连接MySQL数据库  
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    String url="jdbc:mysql://localhost:3306/news"; 
    String user="root"; 
    String password="1234"; 
    Connection conn = DriverManager.getConnection(url, user, password); 
    Statement st = conn.createStatement(); 
     
 %> 
    <table > 
  <tr> 
    <td width="174" id="title">标题</td> 
    <td width="449" id="title">内容</td> 
    <td width="161" id="title">时间</td> 
  </tr> 
     
<% 
    //把表格第二行的显示放到while循环中,就可以根据查询结果画出表格了。参数则放在<td>内的相应位置。 
    ResultSet rs = st.executeQuery("SELECT * FROM data ORDER BY id DESC LIMIT 10"); 
    while(rs.next()){%> 
     
  <tr> 
    <td width="174" ><%=rs.getString("title") %></td> 
    <td width="449" ><%=rs.getString("content") %></td> 
    <td width="161"><%=rs.getString("time") %></td> 
  </tr> 
 
<%} 
//注意"}"的位置 %> 
 </table> 
 
<% 
    rs.close(); 
    conn.close(); 
 %>  
</body> 
</html> 

JSP中的数据库操作(3):JSP页面中的数据库插入

这次是怎样将大象放冰箱的命题了!大家都懂的!

我们要解决的主要有两个问题:

1、如何获取输入的内容

2、如何插入时间。

直接上代码

[html] 
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gb2312"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<%request.setCharacterEncoding("UTF-8");%>  
<%response.setCharacterEncoding("UTF-8");%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<style type="text/css"> 
table{ width:800px; margin:auto; padding: 5px; font-size:12px; border:0px; background:#00CCFF;} 
tr{ background:#fff;} 
td{ padding: 5px;} 
#title{ text-align:center;} 
</style> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>无标题文档11</title> 
</head> 
 
<body> 
 <form method=post> 
    标题:<input type="text" name="title"/><br/> 
    内容:<input type="text" name="content"/><br/> 
    <input type="submit" value="提交" /> 
 </form> 
 <% 
 //连接MySQL数据库  
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    String url="jdbc:mysql://localhost:3306/news"; 
    String user="root"; 
    String password="1234"; 
    Connection conn = DriverManager.getConnection(url, user, password); 
    Statement st = conn.createStatement(); 
     
 %> 
    <table > 
  <tr> 
    <td width="174" id="title">标题</td> 
    <td width="449" id="title">内容</td> 
    <td width="161" id="title">时间</td> 
  </tr> 
     
<% 
     
    ResultSet rs = st.executeQuery("SELECT * FROM data ORDER BY id DESC LIMIT 10"); 
    while(rs.next()){%> 
     
  <tr> 
    <td width="174" ><%=rs.getString("title") %></td> 
    <td width="449" ><%=rs.getString("content") %></td> 
    <td width="161"><%=rs.getString("time") %></td> 
  </tr> 
 
<%} %> 
 </table> 
 
<% 
//通过input中的name获取输入的内容。time那一行是获得时间及定义时间格式 
    String getTitle=request.getParameter("title");  
    String getContent=request.getParameter("content"); 
    java.text.SimpleDateFormat time = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); 
    String insertSQL = "INSERT INTO data(title, content, time) Values ('"+getTitle+"', '"+getContent+"', ' "+time.format(new java.util.Date())+"')"; 
    st.executeUpdate(insertSQL); 
 %> 
<% 
    rs.close(); 
    st.close(); 
    conn.close(); 
 %>  
</body> 
</html> 


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值