Table of Contents
1: Download the database driver jar package to store WebContent —WEB-INF—lib directory
1.2 Steps
2.1 These five pieces of information about the database are essential before establishing a connection
---------(the above 5 information are required)--------
Run jsp File table creation: tt1
Home Java javaTutorial jdbc connection mysql example steps

jdbc connection mysql example steps

Jun 25, 2017 am 10:32 AM
java jdbc mysql web study connect

First let’s take a look at the relationship diagram between the host and the database

It’s actually two servers

1: Download the database driver jar package to store WebContent —WEB-INF—lib directory

1.2 Steps

1.

2.

3

4.

1.3 Import the jar package into WebContent—WEB-INF —Under the lib directory

                                                                                                                                   Manually copy the database driver jar package and save it to the lib directory

2. Create a new jsp file to access the database

2.1 These five pieces of information about the database are essential before establishing a connection

      

1. Database information:

IP:127.0.0.1

port: 3306

Library name: test2

User name :root

Password: 123456

---------(the above 5 information are required)--------

Run jsp File table creation: tt1

The code is as follows:

 1 <%@ page import="org.gjt.mm.mysql.Driver"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3     pageEncoding="UTF-8"%> 4 <%@ page import="java.sql.Connection,java.sql.DriverManager" %> 5 <%@ page import="java.sql.PreparedStatement" %> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">10 <title>Insert title here</title>11 </head>12 <body>13 <%14  //加载数据库驱动jdbc15  Class.forName("org.gjt.mm.mysql.Driver");  
16  out.print("成功加载驱动");17  String url="jdbc:mysql://127.0.0.1:3306/zhz?user=root&password=none";18  //获取数据库链接,让Java可以操作mysql19  Connection conn=DriverManager.getConnection(url);20  //定义一个sql命令21  String sql="create table tt1(sno varchar(20),name varchar(20),birth date)"; 
22  //从connection对象中,获取一个sql执行者命名为pr23  PreparedStatement pr=conn.prepareStatement(sql);24  //执行26  pr.execute();27  out.print(conn);28  //完成操作后关闭数据库链接29  pr.close();30  conn.close();31 %>32 </body>33 </html>
Copy after login

We create it in the zhz database through jsp code Create a table named tt1

Will

String sql="create table tt1(sno varchar(20),name varchar(20),birth date)";
改为
String sql="insert into tt1 values('2','张三','1998-08-21')";
运行jsp文件,向表中添加数据
dos命令查看,效果图
Copy after login

At this time the application service and mysql have been established Connected

<br><br><br>
Copy after login

The above is the detailed content of jdbc connection mysql example steps. For more information, please follow other related articles on 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 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)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

See all articles