JSP和MySQL串連

來源:互聯網
上載者:User

 中文問題:

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

 }
 %>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.