国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開發(fā) 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>

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!


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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 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)