JavaWeb基础学习11(JDBC回顾)

JDBC

什么是JDBC:等价于中间商,将Application和数据库连接起来

  1. 需要的包
<!-- 连接mysql数据库 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>
<!-- 以及java.sql包和jakarta.sql包 -->
  1. 连接编辑MySQL
// TODO Auto-generated method stub
		 String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
		 String user = "root";
		 String password = "123456";
		//加载驱动
		 Class.forName("com.mysql.cj.jdbc.Driver");
		 //连接数据库
		 Connection connection = DriverManager.getConnection(url,user,password);
		 //向数据库发送SQL的对象statement
		 Statement statement = connection.createStatement();
		 
		 //编写SQL
		String sql = "select * from users;";
		ResultSet resultSet = statement.executeQuery(sql);
		 
		while(resultSet.next()) {
			System.out.print("id="+resultSet.getObject("id")+"\t");
			System.out.print("name="+resultSet.getObject("name")+"\t");
			System.out.print("password="+resultSet.getObject("password")+"\t");
			System.out.print("email="+resultSet.getObject("email")+"\t");
			System.out.print("birthday="+resultSet.getObject("birthday")+"\t");
			System.out.println();
		}
		 //关闭,先开后关
		resultSet.close();
		statement.close();
		connection.close();
	}
  1. 用预编译编写
public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		 String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
		 String user = "root";
		 String password = "123456";
		//加载驱动
		 Class.forName("com.mysql.cj.jdbc.Driver");
		 //连接数据库
		 Connection connection = DriverManager.getConnection(url,user,password);
		 //向数据库发送SQL的对象statement
		 //编写SQL
		 String sql = "insert into users(id,name,password,email,birthday)value(?,?,?,?,?);";
		 //预编译
		 PreparedStatement statement = connection.prepareStatement(sql);
		 
		 statement.setInt(1, 4);
		 statement.setString(2, "五六七");
		 statement.setString(3, "123456");
		 statement.setString(4, "null");
		 statement.setDate(5,new Date(new java.util.Date().getDate()));
		 
		 
		 int update = statement.executeUpdate();
		 if(update>0) {
			 System.out.println("成功注册");
		 }
		 //关闭,先开后关
		
		statement.close();
		connection.close();
	}

}
用Servlet编写连接MySQL

用Servlet编写JDBC可以在maven包中实现前端与数据库的连接

1.编写一个servlet

public void init() {
		 String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
		 String user = "root";
		 String password = "123456";
		//加载驱动
		 try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 //连接数据库
		 try {
			connection = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String id = request.getParameter("id");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String birthday = request.getParameter("birthday");
		 String sql = "insert into users(id,name,password,email,birthday)value(?,?,?,?,?);";
		 //预编译
		 PreparedStatement statement;
		try {
			statement = connection.prepareStatement(sql);
			 statement.setString(1, id);
			 statement.setString(2, username);
			 statement.setString(3, password);
			 statement.setString(4, email);
			 statement.setString(5,birthday);
			int update = statement.executeUpdate();
			 if(update>0) {
				 System.out.println("成功注册");
			 }
			//关闭,先开后关
				statement.close();
				connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		response.sendRedirect("/bbb/login.jsp");	
	}
  1. 编写一个jsp文件
<form action="http://localhost:8080/bbb/Longin" mathod="post">
<h3>注册:</h3>
<p>
ID:<input type="number" name="id">
名字:<input type="text" name="username"><br>
密码:<input type="password" name="password">
Email:<input type="text" name="email"><br>
生日:<input type="date" name="date"><br>
<input type="submit">
  1. 编辑web.xml
  <servlet>
    <description></description>
    <display-name>LonginServlet</display-name>
    <servlet-name>LonginServlet</servlet-name>
    <servlet-class>com.hai.LonginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LonginServlet</servlet-name>
    <url-pattern>/Longin</url-pattern>
  </servlet-mapping>

测试:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海量的海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值