我上网查,说是占用了8080端口,但是8080端口并未占用,仍出现404错误,我昨天刚开始学习JAVAweb,然后,这个404错误真心不会解决,请问有人能帮我看看吗,谢谢
试试在.xml文件里加入
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
在resources文件夹下新建db.properties用于存放数据库的配置及文件(驱动,路径,用户名和密码)
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8,使用安全连接,可以将数据类型为Date的字段设置为空值
url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=true&zeroDateTimeBehavior=convertToNull
username=root
password=123456
#连接数据库的驱动
driver=com.mysql.cj.jdbc.Driver
新建DBDao类,编写数据库的公共类就是将连接数据库和关闭资源和查询操作设置为静态方法或静态类型,方便用户重复的调用方法,极大的提高了项目增删查改的效率。
public class DBDao {
private static String url;
private static String username;
private static String password;
private static String driver;
static {
//读取db.properties的数据转变为数据流
InputStream is = DBDao.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties=new Properties();
try {
properties.load(is);//加载数据
} catch (IOException e) {
e.printStackTrace();
}
//获取各项资源
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
driver=properties.getProperty("driver");
}
//数据库连接操作
public static Connection getCon(){
Connection con=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
//查询操作
public static ResultSet executeQuery(Connection con, PreparedStatement ps,ResultSet rst,String sql,Object[] params) throws SQLException {
ps=con.prepareStatement(sql);//预编译
for (int i = 0; i <params.length; i++) {
//占位符从1开始,但是数组是从0开始的
ps.setObject(i+1,params[i]);
}
rst=ps.executeQuery();
return rst;//返回结果集
}
//增加删除修改操作
public static int executeUpdate(Connection con,PreparedStatement ps,String sql,Object[] params) throws SQLException {
ps=con.prepareStatement(sql);
for (int i = 0; i <params.length ; i++) {
ps.setObject(i+1,params[i]);
}
int row=ps.executeUpdate();
return row;//返回受影响的行数
}
//数据库关闭操作
public static void getClose(Connection con,PreparedStatement ps,ResultSet rst){
if (rst!=null){//若不为空,执行关闭操作
try {
rst.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (con!=null){
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}