JSP connection MySQL Database problem

Source: Internet
Author: User
Tags stmt

Before writing a large paragraph, also said a specific JDBC Connection database API specific package of basic knowledge, know their hands accidentally pressed the delete button. As a result, when you go to the auto-save record, the following is the only time left. Well, the main core is the bottom point. Specific as follows:

For example, the following connection MySQL database example:

Class.forName ("Com.mtsql.jdbc.Driver"); Load MySQL Database driver

String url = "jdbc:mysql://localhost:3306/DataBase ' Name"; MySQL specific data connection address

Connection con = drivermanager.getconnection (Url,user,password);///remainder specify data to establish a connection

Statement stmt=con.createstatement (resultset.type_scroll_sensitive,resultset.cuncur_updatatable);//Database operation

First, following the example above, I give you an example of a MySQL database directly connected to the MyEclipse. Here I use the database name is Fine_food_system, my database user name and password are root. The specific code is as follows:

1  PackageMySQL;2 3 ImportJava.sql.*;4 5  Public classJdbctest {6      Public Static voidMain (String args[]) {7String url= "Jdbc:mysql://127.0.0.1:3306/fine_food_system";//establish a specific data connection address8Connection con=NULL;//database connection, currently set to null9Statement sm=NULL;//database query status, currently set to nullTenResultSet rs=NULL;//database operation execution result access, currently set to null One         Try{ AClass.forName ("Com.mysql.jdbc.Driver");//load MySQL database driver -SYSTEM.OUT.PRINTLN ("Load driver succeeded"); -         } the         Catch(Exception e) { -System.out.println ("Can Not load Driver"); -             return; -         } +          -         Try{ +con = drivermanager.getconnection (URL, "root", "root");//connect to MySQL database ASM = Con.createstatement ();//a specific container for executing related SQL statements atrs = Sm.executequery ("SELECT * from client");//execute the SQL statement in parentheses and save the result to Rs -System.out.println ("Result of select Statement"); -              while(Rs.next ()) {//The query results in RS may contain multiple lines, each time the loop is executed, the next row of results is automatically searched -String Clientno = rs.getstring ("Clientno");//get customer number in query results -String clientName = rs.getstring ("ClientName");//get customer name in query results -String Clienttel = rs.getstring ("Clienttel"); inString clientsite = rs.getstring ("Clientsite");//get query results at customer locations -String clientpwd = rs.getstring ("Clentpwd"); toSystem.out.println ("Clientno:" +Clientno); +System.out.println ("ClientName:" +clientName); -System.out.println ("Clienttel:" +Clienttel); theSystem.out.println ("Clientsite:" +clientsite); *System.out.println ("Clientpwd:" +clientpwd); $ System.out.println ();Panax Notoginseng             } - rs.close (); the sm.close (); + con.close (); A         } the         Catch(SQLException ex) { +SYSTEM.OUT.PRINTLN ("SQL Exception!!! "); -         } $     } $  -}

The results of the implementation are as follows:

Driver successfully loaded
Result of SELECT Statement
clientno:1001
ClientName: Ryu Jin
clienttel:15927175020
Clientsite: Zhongnan University of Nationalities
clientpwd:123456

Above said on the MyEclipse directly connected to the MySQL data problem, then the following is the focus of--jsp connection database, JSQ connection database steps have the following points:

(1) Open MySQL, Tomcat service

(2) Start the MySQL data service (directly connected to MySQL data in front of course)

(3) Add a service for your own new JSP project Tomcat (unclear classmate can Baidu check)

(4) Enter a specific URL in the browser to see the final result

The specific code is as follows:

<%@ page language= "Java"Import= "java.util.*,java.sql.*" pageencoding= "GBK"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >String URL= "Jdbc:mysql://127.0.0.1:3306/fine_food_system"; Connection Con=NULL; Statement SM=NULL; ResultSet RS=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); Out.println ("Load driver succeeded"); }        Catch(Exception e) {out.println ("Can Not load Driver"); return; }                Try{con= Drivermanager.getconnection (URL, "root", "root"); SM=con.createstatement (); RS= Sm.executequery ("SELECT * from Client"); System.out.println ("Result of Select Statement");  while(Rs.next ()) {String Clientno= Rs.getstring ("Clientno"); String ClientName= Rs.getstring ("ClientName"); String Clienttel= Rs.getstring ("Clienttel"); String Clientsite= Rs.getstring ("Clientsite"); String clientpwd= Rs.getstring ("Clentpwd"); Out.print ("<br>"); Out.print ("<br>"); Out.println ("Clientno:" +Clientno); Out.print ("<br>"); Out.println ("ClientName:" +clientName); Out.print ("<br>"); Out.println ("Clienttel:" +Clienttel); Out.print ("<br>"); Out.println ("Clientsite:" +clientsite); Out.print ("<br>"); Out.println ("Clientpwd:" +clientpwd); Out.print ("<br>"); Out.print ("<br>");            Out.println ();            } rs.close ();            Sm.close ();        Con.close (); }        Catch(SQLException ex) {System.out.println ("SQL Exception!!! "); }   %>This is my JSP page.<br> </body>

The results of the operation are as follows:

The above example is the result of putting the Java code directly in the JSP. The following example shows an example of a table-based output of the results after a change, as follows:

Code:

<%@ page language= "Java"Import= "Java.util.*,java.sql.*,com.mysql.jdbc.driver" pageencoding= "GBK"%><%String drivername= "Com.mysql.jdbc.Driver"; String UserName= "Root"; String userpwd= "Root"; String DbName= "Fine_food_system"; String URL= "jdbc:mysql://localhost/" +dbname+ "? user=" +username+ "&password=" +userpwd; Class.forName (drivername). newinstance (); Connection Conn=drivermanager.getconnection (URL); Statement stmt=conn.createstatement (); String SQL= "SELECT * from Client"; ResultSet RS=stmt.executequery (SQL);%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Client' s information is:<br><br> <table border= "1" cellspacing= "0" > <tr> <th>clientno</th&  Gt <th>clientName</th> <th>clientTel</th> <th>clientSite</th> <th> Clientpwd</th> </tr> <% while(Rs.next ()) {%> <tr> <td><%=rs.getstring ("Clientno")%></td> <td><%= rs.getstring (" ClientName ")%></td> <td><%= rs.getstring (" Clienttel ")%></td> <td><%= Rs.getstring ("Clientsite")%></td> <td><%= rs.getstring ("Clentpwd")%></td> </tr> & lt;%}%> </table> </center> </body>conn.close (); Stmt.close (); Rs.close ();%>

The results of the operation are as follows:

The above is my own in the last week to learn the JSP connection MySQL database harvest, in the recording of their own learning process, but also hope to help other students ^~^

JSP connection MySQL Database problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.