今天在这里讲一讲怎么使用java连接mysql数据库,下面将使用登录功能讲解
首先我是用的mysql的版本是5.0.22,如下图所示
新建一个数据库,名称为db_example,并且选择当前数据库为db_example,其sql语句为:create database db_example;use db_example;如下图所示
接着新建一个t_user数据表,里面应该包含至少name和password两个字段,一边用户登录,sql语句为:create table t_user(name varchar(20) primary key,password varchar(20) not null);如下图
下面想该表中插入一条数据,sql 语句为:insert into t_user(name,password) value('test','123456'); 如下图所示:
查看表中数据 select * from t_user;
到此为止,数据库和数据库表已经建好了,下面开始写jsp登录页面
打开eclipse新建一个web项目,file-->new-->other,选择如下
接下来填写项目名,选择tomcat服务器,注意这里选择2.5,之后点击finish按钮,生成项目。
下面新建一个jsp文件,命名为index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<form action="login" method="post">
输入用户名:<input type='text' value='' name='name' placeholder="这里输入用户名"/><br>
输入密码:<input type="password" name='password' placeholder="这里输入密码"/><br>
<input type="submit" value='登录'/>
</form>
</body>
</html>
运行效果
下面接着写数据库连接代码,在src目录下新建一个java文件DBUtil.java(首先引入连接数据库的jar包,不同数据库版本对应不同的jar包,可以自行百度,jar包放入WebContent-->WEB-INF-->lib目录中)
这里提供我的jar包链接:mysql-connector-java-3.1.12-bin.jar
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
public static String url="jdbc:mysql://127.0.0.1:3306/db_example";
public static String user="root";
public static String password="123456";
public static String drivername="com.mysql.jdbc.Driver";
public static Connection getCon() throws SQLException, ClassNotFoundException{
Class.forName(drivername);
Connection con=DriverManager.getConnection(url, user, password);
return con;
}
public static void closeCon(Connection con) throws SQLException{
if(con!=null){
con.close();
}
}
}
注意上面代码中的url、user、password和drivername的值,url的末尾是数据库名,前面是主机名和mysql的端口号,user和password是访问数据库的用户名和密码,drivername根据jar包的版本号不同而有所不同(自行百度)
下面新建一个servlet,用于处理用户请求LoginServlet.java,同样是java文件,点击file-->new-->other-->在输入框输入servlet-->
点击next,输入classname为LoginServlet
点击next,修改URL mappings为/login,这里为/login的原因是在jsp页面中的form表单的action设置为了login
点击finish,完成,下面是我写好的代码,可以直接使用
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
if(name==null||name.equals("")){
return;
}
if(password==null||password.equals("")){
return;
}
String sql ="select * from t_user where name=? and password=?";
Connection con=null;
try {
con=DBUtil.getCon();
PreparedStatement prestmt=con.prepareStatement(sql);
prestmt.setString(1, name);
prestmt.setString(2, password);
ResultSet rs=prestmt.executeQuery();
if(rs.next()){
System.out.println("name:"+rs.getString(1)+",password:"+rs.getString(2));
}else{
System.out.println("用户名或密码错误");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
DBUtil.closeCon(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
现在重启tomcat服务器,进入登录页面,先属于一个错误的用户名或密码
点击登录后查看eclipse的console控制台
现在输入正确的用户名和密码,即数据库中的数据
点击登录,查看控制台
可以看到第二条记录说明已经登录成功
这里就是所有java访问数据库的全部过程了,还有很多没讲的,例如ajax访问后台啊,jquery的使用啊,网页布局等等。。。。