jsp:
<http>
<body>
<div>
<form action=" login" >
用户名:<input type=" texe" name="username">
密码:<input type="password" name="password">
<input type="submit" value="确认登录">
</form>
</div>
</bodt>
</http>
servlet:
import java.sql.*;
public class DBconn {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
// 创建数据库连接
private void getConnection() {
String DBURL = "com.mysql.jdbc.Driver";
String DBDRIVER = "jdbc:mysql://localhost:3306/数据库名字";
String DBROOT = "root";
String DBPWD = "root";
try {
Class.forName(DBURL);
conn = DriverManager.getConnection(DBDRIVER, DBROOT, DBPWD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 定义增删改
public int exeADU(String sql) {
getConnection();
try {
stmt = conn.createStatement(); // 得到statement对象
int iRS = stmt.executeUpdate(sql);
return iRS;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
// 定义查询方法
public ResultSet exeQuery(String sql) {
getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// 关闭数据库连接
public void exeClose() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
<http>
<body>
<div>
<form action=" login" >
用户名:<input type=" texe" name="username">
密码:<input type="password" name="password">
<input type="submit" value="确认登录">
</form>
</div>
</bodt>
</http>
servlet:
import java.sql.*;
public class DBconn {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
// 创建数据库连接
private void getConnection() {
String DBURL = "com.mysql.jdbc.Driver";
String DBDRIVER = "jdbc:mysql://localhost:3306/数据库名字";
String DBROOT = "root";
String DBPWD = "root";
try {
Class.forName(DBURL);
conn = DriverManager.getConnection(DBDRIVER, DBROOT, DBPWD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 定义增删改
public int exeADU(String sql) {
getConnection();
try {
stmt = conn.createStatement(); // 得到statement对象
int iRS = stmt.executeUpdate(sql);
return iRS;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
// 定义查询方法
public ResultSet exeQuery(String sql) {
getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// 关闭数据库连接
public void exeClose() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}