当前位置: 技术问答>java相关
jb5中的servlet中如何进行数据库的操作!
来源: 互联网 发布时间:2015-03-21
本文导语: 用servlet做一个登陆严整程序,代码如下: import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.*; /** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author * @ve...
用servlet做一个登陆严整程序,代码如下:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class login extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=gb2312";
/**Initialize global variables*/
public void init() throws ServletException {
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "";
try {
name = request.getParameter("name");
}
catch(Exception e) {
e.printStackTrace();
}
String pswd = "";
try {
pswd = request.getParameter("pswd");
}
catch(Exception e) {
e.printStackTrace();
}
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
// defenite database property
String sql = "select * from sys_quanxian where name='"+name+"'" ;
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=netCount"; // use your hostname and port number here
String login = "sa"; // use your login here
String password = "";
String pswdDB = ""; // use your password here
Connection conn = null ; Statement stmt = null ; ResultSet rs = null ;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = java.sql.DriverManager.getConnection(url,login,password);
stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next())
{
pswdDB=rs.getString("password").toString();
if(pswdDB.equals(pswd))
{
//priority=rs.getString("priority").toString();
if(name.equals("min"))
{
//session.putValue("login","min");
//session.putValue("login_pri","0");
stmt.close();
conn.close();
response.sendRedirect("main_admin.jsp");
}
else if(name.equals("wang"))
{
//session.putValue("login","wang");
//session.putValue("login_pri","1");
stmt.close();
conn.close();
response.sendRedirect("main_user.jsp");
}
else if(name.equals("lf"))
{
//session.putValue("login","lf");
//session.putValue("login_pri","2");
stmt.close();
conn.close();
response.sendRedirect("main_user.jsp");
}
if(name.equals("xuxu"))
{
session.putValue("login","xuxu");
session.putValue("login_pri","0");
stmt.close();
conn.close();
response.sendRedirect("main_admin.jsp");
}
if(name.equals("lili"))
{
session.putValue("login","lili");
session.putValue("login_pri","0");
stmt.close();
conn.close();
response.sendRedirect("main_admin.jsp");
}
}
else
{
stmt.close();
conn.close();
response.sendRedirect("index.jsp");
}
}
else
{
stmt.close();
conn.close();
response.sendRedirect("index.jsp");
}
String reqURL = name+" visit from "+request.getRemoteAddr()+" at "+dt.toLocaleString();
System.out.println(reqURL);
out.println("");
out.println("login");
out.println("");
out.println("
out.println("");
}
/**Clean up resources*/
public void destroy() {
}
}
编译时出现以下错误:
"login.java": Error #: 360 : unreported exception: java.lang.ClassNotFoundException; must be caught or declared to be thrown at line 47, column 11
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 48, column 35
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 49, column 17
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 50, column 13
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 52, column 11
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 54, column 12
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 62, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 63, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 70, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 71, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 78, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 79, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 86, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 87, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 94, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 95, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 101, column 8
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 102, column 8
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 108, column 8
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 109, column 8
请高手指教!另在jb5中写servlet应该注意什么配置?
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class login extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=gb2312";
/**Initialize global variables*/
public void init() throws ServletException {
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "";
try {
name = request.getParameter("name");
}
catch(Exception e) {
e.printStackTrace();
}
String pswd = "";
try {
pswd = request.getParameter("pswd");
}
catch(Exception e) {
e.printStackTrace();
}
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
// defenite database property
String sql = "select * from sys_quanxian where name='"+name+"'" ;
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=netCount"; // use your hostname and port number here
String login = "sa"; // use your login here
String password = "";
String pswdDB = ""; // use your password here
Connection conn = null ; Statement stmt = null ; ResultSet rs = null ;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = java.sql.DriverManager.getConnection(url,login,password);
stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next())
{
pswdDB=rs.getString("password").toString();
if(pswdDB.equals(pswd))
{
//priority=rs.getString("priority").toString();
if(name.equals("min"))
{
//session.putValue("login","min");
//session.putValue("login_pri","0");
stmt.close();
conn.close();
response.sendRedirect("main_admin.jsp");
}
else if(name.equals("wang"))
{
//session.putValue("login","wang");
//session.putValue("login_pri","1");
stmt.close();
conn.close();
response.sendRedirect("main_user.jsp");
}
else if(name.equals("lf"))
{
//session.putValue("login","lf");
//session.putValue("login_pri","2");
stmt.close();
conn.close();
response.sendRedirect("main_user.jsp");
}
if(name.equals("xuxu"))
{
session.putValue("login","xuxu");
session.putValue("login_pri","0");
stmt.close();
conn.close();
response.sendRedirect("main_admin.jsp");
}
if(name.equals("lili"))
{
session.putValue("login","lili");
session.putValue("login_pri","0");
stmt.close();
conn.close();
response.sendRedirect("main_admin.jsp");
}
}
else
{
stmt.close();
conn.close();
response.sendRedirect("index.jsp");
}
}
else
{
stmt.close();
conn.close();
response.sendRedirect("index.jsp");
}
String reqURL = name+" visit from "+request.getRemoteAddr()+" at "+dt.toLocaleString();
System.out.println(reqURL);
out.println("");
out.println("login");
out.println("");
out.println("
The servlet has received a POST. This is the reply.
");out.println("");
}
/**Clean up resources*/
public void destroy() {
}
}
编译时出现以下错误:
"login.java": Error #: 360 : unreported exception: java.lang.ClassNotFoundException; must be caught or declared to be thrown at line 47, column 11
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 48, column 35
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 49, column 17
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 50, column 13
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 52, column 11
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 54, column 12
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 62, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 63, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 70, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 71, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 78, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 79, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 86, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 87, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 94, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 95, column 9
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 101, column 8
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 102, column 8
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 108, column 8
"login.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 109, column 8
请高手指教!另在jb5中写servlet应该注意什么配置?
|
String sql = "select * from sys_quanxian where name='"+name+"'" ;
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=netCount"; // use your hostname and port number here
String login = "sa"; // use your login here
String password = "";
String pswdDB = ""; // use your password here
Connection conn = null ; Statement stmt = null ; ResultSet rs = null ;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = java.sql.DriverManager.getConnection(url,login,password);
stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next())
{
pswdDB=rs.getString("password").toString();
if(pswdDB.equals(pswd))
{
//priority=rs.getString("priority").toString();
if(name.equals("min"))
{
//session.putValue("login","min");
//session.putValue("login_pri","0");
stmt.close();
conn.close();
这段东西要try{}catch(SQLException e){...}
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=netCount"; // use your hostname and port number here
String login = "sa"; // use your login here
String password = "";
String pswdDB = ""; // use your password here
Connection conn = null ; Statement stmt = null ; ResultSet rs = null ;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = java.sql.DriverManager.getConnection(url,login,password);
stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next())
{
pswdDB=rs.getString("password").toString();
if(pswdDB.equals(pswd))
{
//priority=rs.getString("priority").toString();
if(name.equals("min"))
{
//session.putValue("login","min");
//session.putValue("login_pri","0");
stmt.close();
conn.close();
这段东西要try{}catch(SQLException e){...}