前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JSP和MySQL连接

JSP和MySQL连接

作者头像
全栈程序员站长
发布2021-06-22 19:55:18
3.4K0
发布2021-06-22 19:55:18
举报

 中文问题:

 jsp和mysql连接的时候,要在每一个jsp文件中加:

 <%@ page contentType=”text/html;charset=gb2312″ %>  <meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>

 如果是表单的提交要加上:

 jdbc:mysql://127.0.0.1:3306/publish?useUnicode=true&characterEncoding=GBK” 和

 <% request.setCharacterEncoding(“GBK”); %>

 ############################################################################

 1.

 <%@ page contentType=”text/html;charset=gb2312″ %>  <meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>

 <%  java.sql.Connection sqlConn;  java.sql.Statement sqlStmt;  java.sql.ResultSet sqlRst;  //regiester jdbc driver

 String DBUser=”root”;  String DBPassword=”root”;  String DBServer=”127.0.0.1″; // Can’t use localhost , you must use IP or CNAME  String DBNAME=”publish”; // change to your db name

 Class.forName(“org.gjt.mm.mysql.Driver”).newInstance();  //connect to the database  sqlConn=java.sql.DriverManager.getConnection  (“jdbc:mysql://”+DBServer+”:3306/”+DBNAME+”?useUnicode=true&characterEncoding=gb2312″,

 DBUser,DBPassword);  //create the driver handle  sqlStmt=sqlConn.createStatement

 (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);  //excute sql  sqlRst=sqlStmt.executeQuery(“select * from book”);  %>  <html>  <head>  <title>JSP-JDBC-MYSQL TESTING</title>  </head>  <body>  <table border=”1″ cellspace =”0″ align=”center”>

 <tr><th>ID </th>  <th>Book name </th></tr>  <% while(sqlRst.next()) {%>  <tr>  <td> <%=sqlRst.getString(“id”)%></td>  <td><%=sqlRst.getString(“title”)%></td>  </tr>  <%}%>  </table>  </body>  </html>  <%  //sqlRst.close();  sqlStmt.close();  sqlConn.close();  %>

 ………………………………………………………………………………

 2.

 <%@ page contentType=”text/html;charset=gb2312″ %>  <%  java.sql.Connection conn;  java.lang.String strConn;  Class.forName(“org.gjt.mm.mysql.Driver”).newInstance();  conn= java.sql.DriverManager.getConnection(“jdbc:mysql://127.0.0.1/test”,”root”,””);  %>

 ………………………………………………………………………………

 3.

 <%@ page import=”java.sql.*” %>  <%  out.println(” Border is me !”);

 try {  Class.forName(“org.gjt.mm.mysql.Driver”).newInstance();

 }  catch (Exception E) {  out.println(“Unable to load driver.”);

 }  %>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101214.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档