web、Java项目连接Oracle数据库
参考博客链接:https://blog.csdn.net/qq_39231769/article/details/99675894
(此为MySQL数据库)
没学过Java EE,不了解这种语言是怎么的原理和情况。本次尝试使用eclipse,但是不是for java developers,而是第二个for Javaee developers(名字可能有些不一样)但是图标一样就好。
懒得截图,有细节问题可以联系我扣扣 嘿嘿嘿
新建一个Dynamic web project,加入Javaee工作集(不会可观看其他csdn文章~),最后记得选定产生初始web.xml文件
没选定自己创建一个web.xml
然后配置Oracle的连接驱动jar包(ojdbc6.jar,路径为安装目录下的product\11.2.0\dbhome_1\jdbc\lib),右击项目 -> build path -> add enternal ar…那一个选项,将ojdbc6.jar导入(此处可百度“如何将Oracle连接驱动包导入eclipse”)。
还有要配置tomcat,我的是tomcat8.0,可以去看看自己jdk对应的tomcat版本然后去官网下载,最后导入,因为Tomcat作为web项目的容器,同样可以百度(“Tomcat安装以及配置,导入eclipse”)高版本的不用配置path系统变量
在Oracle数据库中新建用户、新建表,虽然代码有点麻烦,注意如果有的列设置为NOT NULL ,在处理数据的时候一定不要忘记给值,否则会报错。
再web-inf下新建
index.html(主页面)
login.html(登陆界面)
register.html(注册界面)
success.html(登录成功界面)
在src下建包,新建三个类:
Conn.java
Userfrom.java
Login.java
代码:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="login.html"><button>登录</button>
<br></a>
<br>
<a href="register.html"><button>注册</button></a>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Login" method="post">
账号:<input type="text" name="username"> 密码:<input
type="password" name="password"> <input type="submit"
value="登录"><input type="reset" value="重置">
</form>
</body>
</html>
register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="UserFrom" method="post">
用户名:<input type="text" name="username"><br> 密码:<input
type="password" name="password"><br> 生日:<input
type="date" name="brithday"><br> 性别:<input type="radio"
name="sex" value="男">男<input type="radio" name="sex"
value="女">女<br> <input type="submit" value="注册"><input
type="reset" value="重置">
</form>
</body>
</html>
生日可以设置为date类型,也可以varchar2类型
这时候要配置服务,导入Tomcat,即WebServlet,Java代码中的WebServlet和html中的action要对应
Conn.java
package com.Oracleconn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn {
static String driverClass = "oracle.jdbc.OracleDriver";
static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
static String user = "Oliver";
static String password = "111111";
public static Connection getconn() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url,user,password);
System.out.println("连接Oracle成功!");
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
return conn;
}
}
Userfrom.java
package com.Oracleconn;
import java.io.IOException;
import java.util.Random;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/UserFrom")
public class Userfrom extends HttpServlet {
private static final long serialVersionUID = 1L;
int i = new Random().nextInt(50)+1;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求的字符集
request.setCharacterEncoding("utf-8");
// 设置相应的文本类型
response.setContentType("text/html;charset=utf-8");
// 数据库工具类
// MysqlTool to=new MysqlTool();
String username = request.getParameter("username");
String password = request.getParameter("password");
String brithday = request.getParameter("brithday");
String sex = request.getParameter("sex");
Connection conn = null;
PreparedStatement ps = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
if (username == null || password == null || brithday == null || sex == null) {
response.sendRedirect("index.html");
} else {
// 1、获取连接对象
// ++i;
try {
conn = Conn.getconn();
System.out.println(username + "\t" + password + "\t" + brithday + "\t" + sex);
String sql1 = "select id from users where uname = ?";
// sql1.setString(1,username);
ps = conn.prepareStatement(sql1);
ps.setString(1, username);
// Object[] objs1 = { username };
// rs = st.query(sql1, objs1);
rs = ps.executeQuery();
if (rs.next()) {
response.sendRedirect("register.html");
} else {
String sql2 = "insert into users(uname,password,brithday,sex,id)values(?,?,?,?,?)";
// Object[] objs2 = { username, password, brithday, sex };
ps1 = conn.prepareStatement(sql2);
ps1.setString(1, username);
ps1.setString(2, password);
ps1.setString(3, brithday);
ps1.setString(4, sex);
ps1.setInt(5, i++);
int a = ps1.executeUpdate();
if (a > 0) {
response.sendRedirect("index.html");
} else {
response.sendRedirect("register.html");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Login.java
package com.Oracleconn;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// MysqlTool to=new MysqlTool();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username == null || password == null) {
// 重新登录
System.out.println("账号或密码错误!");
response.sendRedirect("login.html");
} else {
try {
conn = Conn.getconn();
String sql = "select * from users where uname= ? and password=?";
// Object[] objs={username,password};
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
// 登陆成功,跳转到success页面
System.out.println("登陆成功!!");
request.getRequestDispatcher("success.html").forward(request, response);
/* response.sendRedirect("success.html"); */
} else {
System.out.println("账号或密码错误!");
response.sendRedirect("login.html");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
代码可能有些小瑕疵,按照自己情况改一改呀~~~
运行注册好多次之后的数据库情况。
感谢指正!!!!!!