Home  >  Article  >  Java  >  jdbc connection mysql example steps

jdbc connection mysql example steps

零下一度
零下一度Original
2017-06-25 10:32:141124browse

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>

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命令查看,效果图

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

<br><br><br>

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