Home > Back-end >  On Java to connect mysql mysql or JSP is not successful harvest
On Java to connect mysql mysql or JSP is not successful harvest

Time:11-08

I use notepad + + programming in JavaSE, so connect to the database in the integration tools, also see other information,

1. The corresponding database driver is added to the CLASSPATH environment variable
The driver is a jar package, for example: mysql connector - Java - 8.0.20. Jar

2. The important code in the source code is as follows:
Class.forname (". Com. Mysql. Cj. JDBC Driver ");
The Connection conn=DriverManager. GetConnection (" JDBC: mysql://127.0.0.1:3306/database name? ServerTimezone=GMT ", "user name", "password");

Remember to import the package import Java. SQL. *; The port number 3306, should be set to set when you install mysql port, user name and password should enter your, and check whether the write right,
So that we can successfully commonly,


Connect the mysql database in JavaEE, JSP will refer to the above, I use the tomcat server

1. Don't need to add the database driver to the variable, should be on your web application lib folder in the web - INF folder, if you do this, the driver can only be used in the application, used for all web applications, please drive in the tomcat lib folder,

2. In the JSP, also use the above example code, but one of the classes, should write comprehensive, for example: Java. SQL. Connection conn=Java. SQL. DriverManager. GetConnection (" JDBC: mysql://127.0.0.1:3306/database name? ServerTimezone=GMT ", "user name", "password");

Ok, it is these things,
  • Related