JSP project with MySQL link

Source: Internet
Author: User
Tags tomcat server

Because the completion of the underground garage management system, so the intention to learn JSP development ~

Today is mainly "new Web site project +mysql Link", the work done before in this article: Tomcat server configuration, MySQL database installation and activation (in later development can use the Mysql-front graphical tool for SQL additions and deletions, lazy people + Technical slag one), download the database driver file (Mysql-connector-java).

Create a new Dynamic Web site project in Eclipse by File-->new-->dynamic Web Project. The following box pops up:

link with MySQL : After the project name (I was named carpark in the previous step)-->java resources-->src, and then right-click-->new-->class, the following box pops up:

The Dbutil.java is then automatically generated, and the file is edited with the following code:

Package Com.carpark.util;public class Dbutil {private static String URL = "Jdbc:mysql://localhost:3306/carpark";p rivate    static string driver= "Com.mysql.jdbc.Driver";p rivate static string username = "XXX";    Database login Information private static String password = "xxx"; private static Connection conn;//load driver//try-catch can be automatically generated by eclipse static {try {Class.forName (driver);} catch (CLASSNOTFO Undexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Get database connection public static Connection getconnection () {try {conn = drivermanager.getconnection (Url,username,password);} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Return conn;} Close database public static void close (Connection conn) {if (conn! = null) {try {conn.close ();} catch (SQLException e) {//TODO Auto -generated catch Blocke.printstacktrace ();}}}
Test public static void main (string[] args) {Connection conn = dbutil.getconnection (); SYSTEM.OUT.PRINTLN (conn = = null);}}

After that, the database driver (Mysql-connector-java-x.x.x-bin.jar) needs to be loaded into the project before it can be run correctly, as follows:

Eclipse-->run-->run as, select Java application, Output "False" in the console window to indicate that the database connection is correct ~

  

JSP project with MySQL link

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.