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