您的位置:首页 > 编程语言 > Java开发

Java运用JDBC技术连接Mysql数据库

2017-01-19 17:16 423 查看

一:建立动态web项目

         (1)建立项目



         (2)导入JDBC连接mysql相应jar包

首先登录官网下载相应jar包:http://dev.mysql.com/downloads/connector/j/5.0.html



点击Download



下载好之后解压找到相应jar包:



复制,然后找到项目里的WebContent--WEB-INF--lib,粘贴到lib下



         (3)新建login.jsp文件(名字自取,我的是login.jsp)



         (4)新建servlet(名字自取,我的是loginServlet)





         (5)新建mysql连接类

二:编写代码

        (1):login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="loginServlet" method="post">
账号:<input type="text" id="name" name="name"/><br>
密码:<input type="text" id="password" name="password"><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>


     (2):connectJdbc.java(mysql连接类)

package com.ustcinfi.cn;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connectJdbc {

public static Connection getConn(){
//定义自己的驱动(比如在此项目连接的是mysql)
String driver = "com.mysql.jdbc.Driver";
//数据库的地址,我的是本地地址,?后面部分是编码方式,不加有可能会中文乱码
String url = "jdbc:mysql://localhost:3306/cj?useUnicode=true&characterEncoding=UTF-8";
//连接用户名
String username = "root";
//连接密码(没有安全性要求简单点吧,以防忘记)
String password = "root";
Connection conn = null;
try {
Class.forName(driver); //classLoader,加载对应驱动
conn = (Connection) DriverManager.getConnection(url, username, password);//实现数据库连接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//插入数据
}
(3)loginServlet.java

package com.test.cn;

import java.io.IOException;
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;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
* Servlet implementation class loginServlet
*/
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
connectJdbc connect = null;
@SuppressWarnings("static-access")
Connection conn = connect.getConn();
String sql = "select * from user where username='";
sql+=name+"'and password='"+password+"'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
System.out.println("============================");
System.out.println(sql);
System.out.println("============================");
if(!rs.next()){
response.getOutputStream().write(("登陆失败!用户名或密码错误").getBytes());
}else{
response.getOutputStream().write((name+"登陆成功!").getBytes());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}


三:测试

启动tomcat服务器———浏览器输入:http://localhost:8080/JdbcLogin/login.jsp



需要注意的是我写的sql里面有一张叫user的表,里面有username和password两个字段,且在数据库里面有一条数据是:username=“admin”,password=“admin”。

所以我在这里输入admin,admin会有正确结果,其他的就登陆失败。





大概就是这样了,大家可以试一试,有问题欢迎来问!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息