Installing and loading the JDBC driver
Put the file mysql-connector-java-5.1.7-bin.jar in the lib file in the project WEB-INF directory, and the installation is complete (The premise is that MySQL has been installed on your machine. If it is not installed, install it first)
Load in the JDBC driver
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>加載JDBC驅動程序</title> </head> <body> <% try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序 }catch(ClassNotFoundException e){ out.println("找不到驅動類");//拋出異常時,提示信息 } %> </body> </html>
Connect to the MySQL database
Start Mysql and Tomcat,
Use JDBC to connect to the database.
The first way
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <title>鏈接MySQL數(shù)據(jù)庫</title> </head> <body> <% try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb?user=root&password=zhangda890126;;");//鏈接數(shù)據(jù)庫 }catch(ClassNotFoundException e){ out.println("找不到驅動類");//拋出異常時,提示信息 }catch(SQLException e){ out.println("鏈接MySQL數(shù)據(jù)庫失敗");//處理SQLException異常 } %> </body> </html>
The second way
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <title>鏈接MySQL數(shù)據(jù)庫</title> </head> <body> <% String url = "jdbc:mysql://localhost:3306/javaweb";//連接數(shù)據(jù)庫的url地址 String user = "root";//登錄數(shù)據(jù)庫的用戶名 String password = "zhangda890126;;";//登錄數(shù)據(jù)庫的用戶名的密碼 try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序 Connection conn = DriverManager.getConnection(url,user,password);//鏈接數(shù)據(jù)庫 }catch(ClassNotFoundException e){ out.println("找不到驅動類");//拋出異常時,提示信息 }catch(SQLException e){ out.println("鏈接MySQL數(shù)據(jù)庫失敗");//處理SQLException異常 } %> </body> </html>
For more tutorials on how JSP programs use JDBC to connect to MySQL, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
