您现在的位置是:首页 > 电脑 > 

jsp连接MySQL数据库

2025-07-18 15:32:09
1.jdbc连接数据库步骤         1)导入数据库jar包         2)注册驱动Class.forame(com.mysql.jdbc. Driver);         )创建连接                 String url=jdbc:mysql://localhost:06/数据库名;        

1.jdbc连接数据库步骤

        1)导入数据库jar包

        2)注册驱动Class.forame(jdbc. Driver);

        )创建连接

                String url=jdbc:mysql://localhost:06/数据库名;

                String user=root;//用户名

                String pwd=root;//密码

                Connection con=DriverManager.getConnection(url,user,pwd);

        4)创建执行对象

                Statement stmt = ();

        5)操作数据库

               String sql = select * from 表名 where 1 = 1;

                if (cids!= null && !()){

                         sql =  and cids =   cids   ;

                 }

                if (cnames != null && !()){

                         sql =  and name like %  cnames  % ;

                }

                if (prices != null && !()){

                         sql =  and prices like %  prices  % ;

                }

                println(sql); rs = (sql);

        6)前端显示

                <%=rs.getString(prices)%>

2.实例

数据库名:jdbc

表名:newfiledata

 

<%@ page contentType=text/html;charset=UTF-8 language=java %>
<%@page import=java.sql.* %>

<head>
  <title></title>
  <style type=text/css>
    .item{
      float:left;
      border: solid 1px gray;
      margin-right:60px;
      margin-bottom:25px;
    }
    .item:hover{
      border: solid 1px orange;
      box-shadow: 5px 5px 5px gray;
    }
    .item div{
      margin: 14px;
      line-height: 25px;
    }
    .item div>span:nth-child(){
      color: red;
    }
    .item div span:last-child{
      color: gray;
    }
    .item div input{
      float: right;
    }
  </style>
</head>
<body bgcolor=#2f4f4f>
<%! ResultSet rs;%>
<%
  request.setCharacterEncoding(utf-8);
  String cids = request.getParameter(cids);
  String cnames = request.getParameter(cnames);
  String prices = request.getParameter(prices);

  Class.forame(cj.jdbc.Driver);
  String url=jdbc:mysql://localhost:06/jdbc?serverTimezone=UTC;
  String user=root;
  String pwd=root;
  Connection conn= DriverManager.getConnection(url,user,pwd);
  Statement stmt = ();
  String sql = select * from newfiledata where 1 = 1 ;
  if (cids != null && !()){
    sql =  and cids = 	  cids  	;
  }
  if (cnames != null && !()){
    sql =  and name like 	%  cnames  %	;
  }
  if (prices != null && !()){
    sql =  and prices like 	%  prices  %	;
  }
  println(sql);
  rs = (sql);

%>
<%
  while (()){
%>
<table class=item style=position: relative; left: 240px; width: 250px>
  <td style=width: 15px>价格:<span style=color: red;><%=rs.getString(prices)%></span></td>
  <td><%=rs.getString(cnames)%><br></td>
  <td><%=rs.getString(cids)%><input type=button οnclick=return confirm(	已加入购物车!	) value=购买></td>
</table>
<%
  }
%>
</body>
</html>

 

 

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/551536.html

相关标签:无
上传时间: 2023-06-29 06:42:15
留言与评论(共有 8 条评论)
本站网友 金水湾花园
10分钟前 发表
//localhost
本站网友 反弹一切诅咒
29分钟前 发表
60px; margin-bottom
本站网友 济南奥体中心
5分钟前 发表
right; } </style> </head> <body bgcolor=#2f4f4f> <%! ResultSet rs;%> <% request.setCharacterEncoding(utf-8); String cids = request.getParameter(cids); String cnames = request.getParameter(cnames); String prices = request.getParameter(prices); Class.forame(cj.jdbc.Driver); String url=jdbc
本站网友 gsi
26分钟前 发表
mysql
本站网友 阿尔滨
7分钟前 发表
last-child{ color
本站网友 鼻黑头
20分钟前 发表
5px 5px 5px gray; } .item div{ margin
本站网友 脚气怎么根治偏方
16分钟前 发表
mysql