遇到了很多问题:
1.字符编码问题
error:Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
解决:String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
主要是由于每个jsp文件的编码方式不同,数据库传过来的参数jsp页面无法识别,导致500错误;
2.数据库操作不对应
怪我数据库每学扎实,insert数据的时候忘记了table后面的参数:insert table(sno,sname) value(?,?);
3.没有进行关联
这个困扰了我很久,驱动也放到了lib文件夹下面,就是一直报错;
解决:右击项目——> build path ——> add jars ——>找到目前项目对应的驱动包的位置即可;
4.java web项目中如何引入css/js代码
//文件的开始需要写这一段,以便写入绝对路径,相对路径容易访问失败
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() +path + "/";
%>
然后head里面加入link即可,我的文档结构 : sy3项目名称/css文件名/query.css
<head>
<link rel = "stylesheet" type = "text/css" href = "/sy3/css/query.css">
</head>
疑问:
① 一个eclipse可以下载多个tomcat么?
答:可以,打开server窗口,需要用哪个tomcat就启动哪个,然后把项目部署到tomcat上,打开网址输入相应的端口号即可。
② 同时下载了mysql与sql server会冲突吗?
答:不会,两个数据库的驱动不一样,把对应的驱动放到lib文件下面,并写入完整的驱动代码即可。
③ 驱动应该怎么下载?版本不对应会影响么?
答:一般没什么影响,我mysql是8.0版本的,驱动是5.0.8版本,可以正常启动。
基本步骤:
① 打开eclipse,新建项目Java web
② 部署驱动到lib文件夹,并与当前的项目关联
③ 写代码
④ 打开server窗口,将项目部署到tomcat中,启动tomcat
⑤ 在浏览器窗户输入ip地址:http://localhost:8080/sy3/mysqlquery.jsp
附:源码
1.mysqlquery.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() +path + "/";
%>
<html>
<head>
<link rel = "stylesheet" type = "text/css" href = "/sy3/css/query.css">
</head>
<body>
<div class = "dv1" align = "center">
<span class="title">MYSQL数据库连接</span>
</div>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
public static final String DBUSER="root";
public static final String DBPASS="imshsyxxarsh";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Class.forName(DBDRIVER); //
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS); //�������Ӷ���
String sql="select * from xs";
pstmt=conn.prepareStatement(sql); //��ӡ�ɾ������
rs=pstmt.executeQuery();
%>
<div class = "dv2" align = "center">
<table class = "table">
<tr style = "background-color:#FF6666; height:60px; color:white;">
<td width = "33%">学号</td>
<td width = "33%">姓名</td>
<td>编辑</td>
</tr>
<%
int num=1;
while(rs.next()){
if(num%2!=0) {
%>
<tr>
<td style = "height:60px;">
<%=rs.getString("sno") %>
</td>
<td>
<%=rs.getString("sname") %>
</td>
<td>
<a class = "lik" href="mysqlupdate.jsp?sno=<%=rs.getString("sno")%>&&sname=<%=rs.getString("sname")%>">update</a> <a class = "lik" href="mysqldelete.jsp?sno=<%=rs.getString("sno")%>">delete</a>
</td>
</tr>
<%
}else {
%>
<tr style = "background-color:#FFCCCC; height:60px;">
<td>
<%=rs.getString("sno") %>
</td>
<td>
<%=rs.getString("sname") %>
</td>
<td>
<a class = "lik" href="mysqlupdate.jsp?sno=<%=rs.getString("sno")%>&&sname=<%=rs.getString("sname")%>">update</a> <a class = "lik" href="mysqldelete.jsp?sno=<%=rs.getString("sno")%>">delete</a>
</td>
</tr>
<%
}
num++;
}
%>
</table>
<br><br>
<div class = "lik2"><a class="title2" href="mysqlinsert.html">添加学生基本信息</a></div>
</div>
<%
rs.close();
pstmt.close();
conn.close();
%>
</body>
</html>
2. mysqldelete.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<%
String sno=request.getParameter("sno");
%>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
public static final String DBUSER="root";
public static final String DBPASS="imshsyxxarsh";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="delete from xs where sno=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,sno);
pstmt.executeUpdate();
%>
<%
pstmt.close();
conn.close();
response.sendRedirect("mysqlquery.jsp");
%>
</body>
</html>
3.mysqlinsert.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<body>
<%
//request.setCharacterEncoding("utf-8");
String sno=request.getParameter("usernum");
String sname=request.getParameter("username");
String sname1="章";
System.out.println(sname1);
sname=new String(sname.getBytes("ISO-8859-1"), "gb2312");
System.out.println(sname);
%>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
public static final String DBUSER="root";
public static final String DBPASS="imshsyxxarsh";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="insert into xs(sno,sname) values(?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,sno);
pstmt.setString(2,sname);
pstmt.executeUpdate();
%>
<%
pstmt.close();
conn.close();
response.sendRedirect("mysqlquery.jsp");
%>
</body>
4.mysqlinsert.html
<head>
<link href = "/sy3/css/query.css" rel = "stylesheet" type = "text/css">
</head>
<body>
<div class = "submit" align = "center">
<form action="mysqlinsert.jsp" method="post">
<table class = "table2">
<tr height = "60px;">
<td width = "150px" align = "right">学号 :</td>
<td><input class = "put" type="text" name="usernum"></td>
</tr>
<tr height = "60px;">
<td align = "right"> 姓名:</td>
<td><input class = "put" type="text" name="username"></td>
</tr>
<tr height = "60px;" align = "center">
<td colspan = "2"><input class = "sub" type="submit" value="提交 "></td>
</tr>
</table>
</form>
</div>
</body>
5.mysqlupdate.jsp
<%@ page language = "java" pageEncoding = "utf-8" %>
<head>
<link href = "/sy3/css/query.css" rel = "stylesheet" type = "text/css">
</head>
<body>
<%
String sname=request.getParameter("sname");
//byte b[]=sname.getBytes("ISO-8859-1");
//sname=new String(b);
sname=new String(sname.getBytes("ISO-8859-1"),"gb2312");
%>
<div class = "submit" align = "center">
<form action="mysqlupdate1.jsp" method="post">
<table class = "table2">
<tr height = "60px;">
<td width = "150px" align = "right">学号 :</td>
<td><input class = "put" type="text" name="usernum" value="<%=request.getParameter("sno")%>"></td>
</tr>
<tr height = "60px;">
<td align = "right"> 姓名:</td>
<td><input class = "put" type="text" name="username" value="<%=sname%>"></td>
</tr>
<tr height = "60px;" align = "center">
<td colspan = "2"><input class = "sub" type="submit" value="提交 "></td>
</tr>
</table>
</form>
</div>
</body>
</html>
6.mysqlupdate1.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String sno=request.getParameter("usernum");
String sname=request.getParameter("username");
%>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
public static final String DBUSER="root";
public static final String DBPASS="imshsyxxarsh";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="update xs set sname=? where sno=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(2,sno);
pstmt.setString(1,sname);
pstmt.executeUpdate();
%>
<%
pstmt.close();
conn.close();
response.sendRedirect("mysqlquery.jsp");
%> <br>
</body>
</html>
7.query.css
@charset "UTF-8";
body{
color:#666666;
}
.title{
display:block;
width:400px;
margin:auto;
margin-top:80px;
color:#666666;
border:2px;
font-size:40px;
font-weight:bold;
}
a{
text-decoration:none;
}
.lik{
color:#993366;
}
.dv1{
width:100%;
height:120px;
border:2px;
}
.dv2{
width:100%;
}
.lik2{
width:200px;
height:35px;
border-radius:14px;
padding-top:5px;
background-color:#FF6666;
}
.title2{
font-size:20px;
font-weight:blod;
color:white;
margin:auto;
}
.table{
width:80%;
border:1px solid #ffcece;
}
.submit{
background:#ffcece;
height:600px;
padding-top:200px;
}
.put{
width:300px;
height: 40px;
}
.sub{
border:none;
background:#FF6666;
width:70px;
height:30px;
color:white;
font-size:15px;
}
.table2{
font-size:15px;
font-weight:bold;
align:center;
width:40%;
}
深深记得老师的话:你就不听我讲,这能学会啦???那魔性的声音,我相信,他不会看的,hhhh,我偏偏要学会,能咋地,丢了老脸又何妨?
告大家一个小秘密:我们东来老师,,,还喷香水,,,,hhhhhhh