JSP database operation and invoking the Java class that you wrote

Source: Internet
Author: User

As a novice in this card for a long time, is actually a small problem

The calling database does not directly call the data with the JSP, because I don't know why the drive is always poorly configured.

Instead, it intends to use Java to access the database, and then let the JSP call the Java interface

Java code:

Java must have a package of its own and cannot use the default package name

Package Test;import java.sql.*; Public classMysql { PublicMysql () {String driver="Com.mysql.jdbc.Driver"; //the URL points to the database name you want to access ScutcsString URL ="Jdbc:mysql://127.0.0.1:3306/ems"; String User="Root"; String Password=""; Try {            //Load DriverClass.forName (driver); Connection Conn=drivermanager.getconnection (URL, user, password); if(!conn.isclosed ()) System. out. println ("succeeded connecting to the database!"); //statement used to execute SQL statementsStatement Statement =conn.createstatement (); //the SQL statement to executeString sql ="SELECT * from Pm2_5"; ResultSet RS=statement.executequery (SQL); System. out. println ("-----------------"); System. out. println ("-----------------"); String name=NULL;  while(Rs.next ()) {name= Rs.getstring ("Mark"); //name = new String (name.getbytes ("iso-8859-1"), "Utf-8"); //Output ResultsSystem. out. println (Rs.getstring ("ID") +"\ t"+name);            } rs.close ();        Conn.close (); } Catch(ClassNotFoundException e) {System. out. println ("Sorry,can ' t find the driver!");           E.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); }    }}

JSP Code:

<%@ page language="Java"Contenttype="text/html; Charset=utf-8"%><%@ page import="java.util.*"import="java.io.IOException"%><%@ page import="test. Mysql"%><% out. println ("haha"); NewMysql ();%>

Console output:

Succeeded connecting to the Database!----------------------------------1e354266e-d5cf-8384-d589-A179f92afee52e354266e-d5cf-8384-d589-A179f92afee53e354266e-d5cf-8384-d589-A179f92afee54e354266e-d5cf-8384-d589-A179f92afee55e354266e-d5cf-8384-d589-A179f92afee56e354266e-d5cf-8384-d589-A179f92afee57e354266e-d5cf-8384-d589-A179f92afee58e354266e-d5cf-8384-d589-A179f92afee59e354266e-d5cf-8384-d589-A179f92afee5Tene354266e-d5cf-8384-d589-A179f92afee5 Onee354266e-d5cf-8384-d589-A179f92afee5 Ae354266e-d5cf-8384-d589-A179f92afee5 -e354266e-d5cf-8384-d589-A179f92afee5 -e354266e-d5cf-8384-d589-A179f92afee5 thee354266e-d5cf-8384-d589-A179f92afee5 -e354266e-d5cf-8384-d589-a179f92afee5

And then encapsulate the Java class on the line ^_^

JSP database operation and invoking the Java class that you wrote

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.