java吧 关注:1,239,535贴子:12,710,680
  • 0回复贴,共1

救救孩子,连不上数据库

只看楼主收藏回复

ConnDB.Java
package shopBeans;
import java.sql.*;
import java.io.*;
import java.util.*;
public class ConnDB
{
public Connection conn=null;
public Statement stmt=null;
public ResultSet rs=null;
private static String dbDriver="com.mysql.jdbc.Driver";
private static String dbUrl="jdbc:mysql://localhost:3306/pet";
private static String dbUser="root";
private static String dbPwd="123456";
//打开数据库连接
public static Connection getConnection()
{
Connection conn=null;
try
{
Class.forName(dbDriver);
conn=DriverManager.getConnection(dbUrl,dbUser,dbPwd);
}
catch(Exception e)
{
e.printStackTrace();
}
if (conn == null)
{
System.err.println("警告:数据库连接失败!");
}
return conn;
}
//读取结果集
public ResultSet doQuery(String sql)
{
try
{
conn=ConnDB.getConnection();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
e.printStackTrace();
}
return rs;
}
//更新数据
public int doUpdate(String sql)
{
int result=0;
try
{
conn=ConnDB.getConnection();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}
catch(SQLException e)
{
result=0;
}
return result;
}
//关闭数据库连接
public void closeConnection()
{
try
{
if (rs!=null)
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if (stmt!=null)
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if (conn!=null)
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jquery动画注册登录页面</title>
<!-- Favicon -->
<link rel="icon" type="image/png" sizes="56x56" href="images/them-logo/favicon-1.png">
<!-- Main style sheet -->
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/style1.css">
<!-- responsive style sheet -->
<link rel="stylesheet" href="css/responsive.css">
</head>
<body>
<!-- Header _________________________________ -->
<section class="header-section">
<div class="top-header">
<div class="container">
<div class="clear-fix">
<ul class="float-left top-header-left">
<li><a href="#"><i class="fa fa-map-marker" aria-hidden="true"></i> Welcome to LeChong</a></li>
<li><a href="#"><i class="fa fa-envelope-o" aria-hidden="true"></i> 注册</a></li>
<li><a href="#"><i class="fa fa-envelope-o" aria-hidden="true"></i> 登录</a></li>
</ul>
<ul class="float-right top-header-right">
<li><a href="#"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
<li><a href="#"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
<li><a href="#"><i class="fa fa-pinterest-p" aria-hidden="true"></i></a></li>
<li><a href="#"><i class="fa fa-google" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>
</div>
<!-- Theme Main Menu ____________________________ -->
<div class="theme-main-menu">
<div class="container">
<div class="main-menu clear-fix">
<div class="search-button-content float-right">
<button class="search b-p-bg-color" id="search-button"><i class="fa fa-search" aria-hidden="true"></i></button>
<div class="search-box tran5s" id="searchWrapper">
<button id="close-button" class="p-color"><i class="fa fa-times" aria-hidden="true"></i></button>
<div class="container">
<img src="images/them-logo/them-main-logo-1.png" alt="Logo">
<form action="#">
<input type="text" placeholder="Search....">
<button class="p-bg-color"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>
</div>
</div> <!-- /.search-box -->
</div> <!-- /.right-content -->
<div class="them-logo float-left"><a href="#"><img src="img/logo(1).png" alt="logo"></a></div>
</div> <!-- / menu-skew-div -->
</div> <!-- /.container main-menu -->
</div> <!-- /.main-menu -->
<div class="materialContainer">
<div class="box">
<form action="login_ok.jsp" method="post">
<div class="title">登录</div>
<div class="input">
<label for="userid">用户名</label>
<input type="text" name="userid" id="userid">
<span class="spin"></span>
</div>
<div class="input">
<label for="password">密码</label>
<input type="password" name="password" id="password">
<span class="spin"></span>
</div>
<div class="button login">
<button>
<span>登录</span>
<i class="fa fa-check"></i>
</button>
</div>
<a href="register.html" class="pass-forgot">没有用户?这里注册</a></form>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/index.js"></script>
</body>
</html>
login_ok.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="shopBeans.ConnDB" %>
<%@ page import="java.sql.*" %>
<%
String c_name=(String)request.getParameter("c_name");
String c_password=(String)request.getParameter("c_password");
String cname=(String) session.getAttribute("c_name");
String name="",pass="";
ConnDB conn=new ConnDB();
if (c_name!=null || c_name!="")
{
try
{
String strSql="select c_name,c_password from customer where c_name='"+c_name+"' and c_password='"+c_password+"'";
ResultSet rsLogin=conn.doQuery(strSql);
while(rsLogin.next())
{
name=rsLogin.getString("c_name");
pass=rsLogin.getString("c_password");
}
}
catch(Exception e)
{}
if(name.equals(c_name) && pass.equals(c_password))
{
session.setAttribute("c_name",c_name);
%>
<%
}
else
{
out.println( "<HTML><HEAD><META http-equiv='refresh' content='2; URL=../index.html' target=Main></HEAD><BODY bgcolor='#FFFFFF'></body><ml>");
%>
<style type="text/css">
<!--
.STYLE10 {
font-size: 14px;
color: #FF0000;
}
.STYLE3 {color: #FF0000; font-size: 12px; }
-->
</style>
<br /><br /><br /><br /><br /><br /><br /><br />
<center>
<table width="300" border="1" bordercolor="#99CCFF" style="border-collapse:collapse" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr bgcolor="#99CCFF">
<td height="13">&nbsp;</td>
</tr>
<tr>
<td height="60" align="center"><span class="STYLE10">用户名或者密码错误,请验证后再输<span class="STYLE3">!</span></span></td>
</tr>
</table>
</center>
<%
}
}
%>


1楼2020-04-21 14:59回复