🍖🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖
🍖🍖
🍖🍖🍖
🍖🍖🍖🍖 作者 : 不良使
🍖🍖🍖🍖🍖 潜力创作新星 华为云享专家
🍖🍖🍖🍖🍖🍖 博客记录学习的思路,项目和错误,寻找志同道合的朋友
🍖🍖🍖🍖🍖🍖🍖 如果觉得有帮助记得一键三连 ┗|`O′|┛ 嗷~~
🍖🍖🍖🍖🍖🍖🍖🍖
🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖
连接数据库无非就是和数据库建立连接,和前面的python连接数据库差不多。
首先在连接Java数据库的时候需要数据库的jar
包,这点不同于python连接数据库
mysql的jar包
下面让我们来看看数据库是如何让连接的,是如何完成数据库的一系列 *** 作,增删改查等…
💖Java连接数据库。 🎶1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
🎶2、获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456");
注意:获取连接很重要,数据的一点点错误都会导致你连接不上数据库。
😃1、localhost
代表本机不用变
😃2、3306
是端口号,即port,一般默认都是3306。
😃3、root
一般指管理员,不用变,直接用即可。
😃4、"123456"
指的是密码,一般大家常用的密码就123、111111、root或者123456几种。
🎶3、获取数据库 *** 作对象
stmt = conn.createStatement();
🎶4、//执行sql
String sql = "select id,name,num,price from testmodel_car";
rs = stmt.executeQuery(sql);
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String num = rs.getString("num");
String price = rs.getString("price");
System.out.println(id + "," + name + "," + num + "," + price);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
注意:
😃1、java"select id,name,num,price from testmodel_car";
,从表 testmodel_car中查询 id,name,num,price四列数据
😃2、rs = stmt.executeQuery(sql);
,获取的对象编译一下sql语句
😃3、String id = rs.getString("id");
,取出id那一列的值赋值给id(String id 中的id可以随便换,只要不和其他变量冲突都是可以的)
🎶5、释放资源
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
不释放资源也行,在上面抛出异常也可以解决
🎶6、完整代码
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test2 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456");
//获取数据库 *** 作对象
stmt = conn.createStatement();
//执行sql
String sql = "select id,name,num,price from testmodel_car";
rs = stmt.executeQuery(sql);
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String num = rs.getString("num");
String price = rs.getString("price");
System.out.println(id + "," + name + "," + num + "," + price);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
💖连接数据库------增加 *** 作
add.jsp
<%--
Created by IntelliJ IDEA.
User: 17331
Date: 2022/3/5
Time: 15:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="h/favicon2.ico" rel="icon" type="image/x-icon"/>
<title>添加信息title>
<style>
body {
background-color:#FDF5E6;
}
style>
<style>
body {
width: 100%;
height: 100%;
background: url('./img/olypic.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
background-attachment: fixed;
}
style>
<style>
input{
width: 20%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid #555;
outline: none;
}
input:focus {
background-color: lightblue;
}
style>
<style>
#boxshadow {
position: relative;
-moz-box-shadow: 1px 2px 4px rgba(0, 0, 0,0.5);
-webkit-box-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
box-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
padding: 10px;
background:#F0F8FF;
border-radius:8px;
}
/* Make the image fit the box */
#boxshadow img {
width: 50%;
border: 0px solid #8a4419;
border-style: inset;
}
#boxshadow::after {
content: '';
position: absolute;
z-index: -1; /* hide shadow behind image */
-webkit-box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3);
box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3);
width: 70%;
left: 15%; /* one half of the remaining 30% */
height: 100px;
bottom: 0;
}
style>
<style>
.button {
background-color:#337AB7; /* Green */
border: none;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
cursor: pointer;
}
.button1 {
background-color: white;
color: black;
border: 2px solid #337AB7;
}
.button1:hover {
background-color: #337AB7;
color: white;
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
}
style>
head>
<body>
<div style="padding:2px;width:1080px;height:470px;margin-top:10%;margin-left:20%;opacity: 0.8" id="boxshadow">
<div>
<img src="img/olypic.jpg" style="float:left;margin-top:5%;">
div>
<div style="float:right;">
<form action="add" method="post">
<p style="margin-right:200px;">
国家名称   <input type="text" name="Country" style="border-radius:8px;width:150px;height:30px;">
p>
<p>
  金牌数     <input type="text" name="Glod_num" style="border-radius:8px;width:150px;height:30px;margin-top:1%">
p>
<p>
  银牌数     <input type="text" name="Silver_num" style="border-radius:8px;width:150px;height:30px;margin-top:1%">
p>
<p>
  铜牌数     <input type="text" name="Copper_num" style="border-radius:8px;width:150px;height:30px;margin-top:1%">
p>
<p>
     id           <input type="text" name="id" style="border-radius:8px;width:150px;height:30px;margin-top:1%">
p>
<input type="submit" value="保存"/><br/>
form>
form>
div>
div>
body>
html>
Add.java
package com.bjpowernode.javaweb.servlet;
import com.bjpowernode.javaweb.utils.DButil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
@WebServlet(name = "add", value = "/add")
public class Add extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
//获取信息,前端页面注册的信息
String country=request.getParameter("Country");
// String logo=request.getParameter("logo");
String Glod_num=request.getParameter("Glod_num");
String Silver_num=request.getParameter("Silver_num");
String Copper_num=request.getParameter("Copper_num");
String id =request.getParameter("id");
// String Sum=request.getParameter("Sum");
//连接数据库,执行insert插入语句
Connection conn = null;
PreparedStatement ps = null;
int count=0;
try {
//注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mofa", "root", "123456");
// TODO 2、开启事务 默认开启 我们将它关闭()自动提交机制关闭
String sql = "insert ignore into medals_table(Country,Glod_num,Silver_num,Copper_num,id) values (?,?,?,?,?);";
ps = conn.prepareStatement(sql);
ps.setString(1, country);
// ps.setString(2, logo);
ps.setString(2, Glod_num);
ps.setString(3, Silver_num);
ps.setString(4, Copper_num);
ps.setString(5, id);
// ps.setString(7, Sum);
//返回一个值,指的是 影响了数据库中多少条记录 主要是判断是否大于0 就是是否印象数据库
count = ps.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
DButil.close(conn,ps,null);
}
if(count==1){
//增加成功 任然跳到列表页面
request.getRequestDispatcher("Take_01").forward(request,response);
}
else{
//删除失败
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
}
增加功能就是通过前端jsp文件text填写数据,然后通过submit提交数据,传递给后端servlet,也就是上面的Add,注意Java文件不要小写,我就没少挨骂
步骤:
😘1、前端jsp文件text写入通过submit提交
😘2、");
out.println(" ");
out.println(" ");
out.println("");
out.println("