• JSP和MySQL连接


     中文问题:

     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.");

     }
     %>

  • 相关阅读:
    SharePoint缓存配置
    修改默认的个人站点
    生成代码工具
    Ext 布局(layout)
    编程网站
    EXT 4.0 上传文件
    修改Ext grid 单元格
    Ext.tip.Tooltip 提示
    EXT iframe 应用
    EXT 4.0 TreeGrid 使用
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/717007.html
Copyright © 2020-2023  润新知