Home php教程 PHP开发 Tutorial on using JSP program to connect to MySQL using JDBC

Tutorial on using JSP program to connect to MySQL using JDBC

Dec 29, 2016 pm 05:15 PM

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>
Copy after login

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数据库</title>
  </head>
  <body>
    <%
      try{
        Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb?user=root&password=zhangda890126;;");//链接数据库
          
      }catch(ClassNotFoundException e){
        out.println("找不到驱动类");//抛出异常时,提示信息
      }catch(SQLException e){
        out.println("链接MySQL数据库失败");//处理SQLException异常
      }
    %>
  </body>
</html>
Copy after login

The second way

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>链接MySQL数据库</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
      String user = "root";//登录数据库的用户名
      String password = "zhangda890126;;";//登录数据库的用户名的密码
      try{
        Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
        Connection conn = DriverManager.getConnection(url,user,password);//链接数据库
          
      }catch(ClassNotFoundException e){
        out.println("找不到驱动类");//抛出异常时,提示信息
      }catch(SQLException e){
        out.println("链接MySQL数据库失败");//处理SQLException异常
      }
    %>
  </body>
</html>
Copy after login

For more tutorials on how JSP programs use JDBC to connect to MySQL, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1276
29
C# Tutorial
1256
24