JSP page connection mysql database

xiaoxiao2021-03-05  19

MySQL driver: mysql-connection-java-3.1.7.tar.gzj2se version: 1.5.0_02tomcat version: 5.0.30Mysql: 4.1.10 or 3.23.49 (I used)

First, decompress the MySQL driver, there is a MySQL-Connector-Java-3.1.7-bin.jar package under /mysql-connector-java-3.1.7. Copy it to

Java installation directory D: / program / javad: / program files / java / jre1.5.0_02 / lib / ext (debugging Java program) and D: / Program files / java / jdk1.5.0_02 / jre / lib / EXT (using the Tomcat program, you can also copy to% catalina% / common / lib)

Another way is to find a directory to store MySQL driver. Add the path to the path in the environment variable.

A Java Application example.

// loadDriverimport java.sql.connection; import java.sql.driverManager; import java.sql.sqlexception; // notice, do not import com.mysql.jdbc. * // or you will have problems!

Public class loaddriver {public static void main (string [] args) {Try {// the newinstance () call is a work aroundancy some // broken java importation

Class.Forname ("com.mysql.jdbc.driver). Newinstance ();} catch (exception ex) {system.out.println (ex);}}}

After debugging, running loadDriver.class If there is no abnormality, the driver configuration of the JavaAplication program is successful.

A JSP example

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "Java.SQL. *"%> <% connection con; statement sql; resultet ket; {Class.Forname ("com.mysql.jdbc.driver). Newinstance ();} catch (classnotfoundexception e) {E.getMessage ();} try {con = drivermanager.getConnection (" JDBC: mysql: // localhost / mysql? user = & password = & useunicode = true & characterencoding = GBK ");

SQL = con.createstatement (); rs = sql.executeQuery ("Select * from user"); out.print ("

"); Out.print (""); out.print (" "); out.print ("
"Name"); out.print ("" "password"); out.print (""); while (rs.next () ) {OUT.PRINT ("
" rs.getstring (1) ""); out.print ("" RS. GetString (2) ""); out.print ("");} out.print (""); con.close ();} catch (Exception E) {E.GetMessage ();}%> file saves to% catalina_home / WebApps / root. Start the Tomcat server, debug files If there is no exception, the configuration is successful.

转载请注明原文地址:https://www.9cbs.com/read-38651.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.038, SQL: 9