解决在eclipse2021中,用mysql-connector-java-8.0.18.jar不兼容,导致无法访问数据库问题(2021-10-23)

解决在eclipse2021中,用mysql-connector-java-8.0.18.jar不兼容,导致无法访问数据库问题

(1)一个jsp Model1模式简单的商品查询为例,

(jsp+javabean+mysql)

  • eclipse2021
  • mysql1.5.6
  • Tomcat9.0
  • jdk 16.0.1
  • mysql-connector-java-8.0.18.jar、mysql-connector-java-5.1.6-bin.jar
  • jar下载
    链接:https://pan.baidu.com/s/1HEc-0yJthDu0H6b9x-1mYQ
    提取码:21bo

问题

如果用mysql-connector-java-8.0.18.jar,设置配置都没问题的情况下,则出现不兼容,报错:
解决在eclipse2021中,用mysql-connector-java-8.0.18.jar不兼容,导致无法访问数据库问题(2021-10-23)
换成mysql-connector-java-5.1.6-bin.jar,则可正常运行且写入:
如图:
解决在eclipse2021中,用mysql-connector-java-8.0.18.jar不兼容,导致无法访问数据库问题(2021-10-23)
解决在eclipse2021中,用mysql-connector-java-8.0.18.jar不兼容,导致无法访问数据库问题(2021-10-23)
由于未去修改字符编码,所以导致中文乱码

(2)代码块:

  • model1index.jsp (位置:Testbean\webapp\model1index.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="model1prosess.jsp" method="post" onsubmit="return save(this);">
 		<table border="1" align="center" width="300" bordercolor="#000000">
 			<tr>
 			    <td align="center" colspan="2">
 			       <br><h1>录入物品信息</h1>
 			    </td>
 			</tr>
 			<tr><td>物品名称</td><td><input type="text" name="name" id="name"></td></tr>
 			<tr><td>价格:</td><td><input type="text" name="price" id="price"></td></tr>
 			<tr><td>物品描述:</td><td><input type="text" name="description" id="description"></td></tr>
 			<tr><td align="center" colspan="2"><input type="submit" value="提交"> <input type="reset" value="重置"></td></tr>
 		</table>
	</form>

</body>
</html>
  • model1prosess.jsp (位置:Testbean\webapp\model1prosess.jsp)
<%@page import="javabean.model1.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% request.setCharacterEncoding("UTF-8"); %>
    <jsp:useBean id="goods" class="javabean.model1.Goods"></jsp:useBean>
    <jsp:setProperty name="goods" property="*" />
    <jsp:useBean id="goodsDao" class="javabean.model1.GoodsDao"></jsp:useBean>
    <% goodsDao.saveGoods(goods); %>
</body>
</html>
  • Goods.java (位置:Testbean\src\javabean\model1\Goods.java)
package javabean.model1;

public class Goods {
	private String name;
	private double price;
	private String description;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
}
  • GoodsDao.java (Testbean\src\javabean\model1\GoodsDao.java)
package javabean.model1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class GoodsDao {
	public void saveGoods(Goods goods){
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动程序
			String url="jdbc:mysql://localhost:3306/javabean";//数据库链接地址
			 Connection conn=DriverManager.getConnection(url,"root","root");//获取数据库链接
			  
			  //定义sql语句
			 String sql="insert into goods(name,price,description) values(?,?,?)";
			 
			 //创建PreparedStatement对象
			 PreparedStatement ps=conn.prepareStatement(sql);
			 
			 //sql参数赋值
			 ps.setString(1,goods.getName());
			 ps.setDouble(2, goods.getPrice());
			 ps.setString(3, goods.getDescription());
			 
			 ps.executeUpdate();//更新操作
			 ps.close();
			 conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  • 数据库 javabean 表:goods
    解决在eclipse2021中,用mysql-connector-java-8.0.18.jar不兼容,导致无法访问数据库问题(2021-10-23)
上一篇:Java基础之BeanInfo


下一篇:缓存与数据库一致性如何解决,JavaBean与xml互转的方法详解