JSP page Connection MySQL database

Source: Internet
Author: User
js|mysql| Data | database | page
MySQL Driver: mysql-connector-java-3.1.7.tar.gz
J2SE version: 5.02
Tomcat version: 5.0.30
mysql:4.1.10 or 3.23.49 (the version I use)
First, to extract 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\Java
D:\Program files\java\jre1.5.0_02\lib\ext (used by debugging Java programs)
and D:\Program Files\java\jdk1.5.0_02\jre\lib\ext (Tomcat program used, can also be copied to%catalina%\common\lib)
Another way is to find a directory to store the MySQL driver. Add paths to the path in the system's environment variables.
A Java application example.
Loaddriver
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Notice, do not import com.mysql.jdbc.*
Or you'll have problems!
public class Loaddriver {
public static void Main (string[] args) {
try {
The newinstance () call are a work around for some
Broken Java implementations
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
}
catch (Exception ex) {
System.out.println (ex);
}
}
}
After debugging, run Loaddriver.class If no exception is thrown, the driver configuration of the Javaaplication program is successful
A JSP example
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<body>
<% Connection con;
Statement SQL;
ResultSet rs;
try{
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
}
catch (ClassNotFoundException e) {
E.getmessage ();
}
try{
Con=drivermanager.getconnection ("Jdbc:mysql://localhost/mysql?")
USER=&AMP;PASSWORD=&AMP;USEUNICODE=TRUE&AMP;CHARACTERENCODING=GBK ");
Sql=con.createstatement ();
Rs=sql.executequery ("SELECT * from user");
Out.print ("<table border>");
Out.print ("<tr>");
Out.print ("<th width=100>" + "name");
Out.print ("<th width=100>" + "password");
Out.print ("</tr>");
while (Rs.next ()) {
Out.print ("<TR>");
Out.print ("<TD>" +rs.getstring (1) + "</TD>");
Out.print ("<TD>" +rs.getstring (2) + "</TD>");
Out.print ("</TR>");
}
Out.print ("</Table>");
Con.close ();
}
catch (Exception e) {
E.getmessage ();
}
%>
</body>
The file is saved to%catalina_home%\webapps\root. Start Tomcat server, debug file is configured successfully if no exception occurs

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.