当前位置: 技术问答>java相关
怎么样连接数据库阿!!!有没有好的教程或实例阿
来源: 互联网 发布时间:2015-02-26
本文导语: | /* /*@ author:yjx /*@ date:2001-7-9 */ /*import package file*/ import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.util.*; import java.lang.*; public class Loginservlet extends HttpServlet { public void doPost...
|
/*
/*@ author:yjx
/*@ date:2001-7-9
*/
/*import package file*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.lang.*;
public class Loginservlet extends HttpServlet
{
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,java.io.IOException
{
PrintWriter out;
String strUserName,strPWD;
String strDBDriver,strURL;
String strSQL,strTemp;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
int iExist=0;//0:not exits,1:exits
HttpSession session;
session=req.getSession();
out = resp.getWriter();
//get parameters
strUserName=req.getParameter("username");
strPWD=req.getParameter("password");
//set database connection parameter value
strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
strURL="jdbc:odbc:wygl";
if(strUserName!=null || strUserName.compareTo("")!=0){
//instance session variable:
session.putValue("login_name","");
session.putValue("user_name","");
session.putValue("dept_id","");
session.putValue("duty","");
session.putValue("mright","");
session.putValue("uitype","");
session.putValue("logined","false");
session.putValue("info","null");
//connect database
try{
Class.forName(strDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println("error:"+e.getMessage());
}
try{
conn=DriverManager.getConnection(strURL);
stmt=conn.createStatement();
}catch(SQLException e){
System.out.println("error:"+e.getMessage());
}
strSQL="select * from sys_user where login_name='"+strUserName+"' and user_pwd='"+strPWD+"'";
try{
rs=stmt.executeQuery(strSQL);
if(rs.next()){
session.putValue("login_name",strUserName);
session.putValue("logined","true");
strTemp=rs.getString("user_name");
session.putValue("user_name",strTemp);
strTemp=rs.getString("dept_id");
session.putValue("dept_id",strTemp);
strTemp=rs.getString("duty");
session.putValue("duty",strTemp);
strTemp=rs.getString("mright");
session.putValue("mright",strTemp);
strTemp=rs.getString("uitype");
session.putValue("uitype",strTemp);
resp.sendRedirect("/wygl/jsp/sysindex.jsp");
}else{
resp.sendRedirect("/wygl/jsp/errorindex.jsp");
}
}catch(SQLException e){
System.err.println("error:"+e.getMessage());
}
}
}
}
/*@ author:yjx
/*@ date:2001-7-9
*/
/*import package file*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.lang.*;
public class Loginservlet extends HttpServlet
{
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,java.io.IOException
{
PrintWriter out;
String strUserName,strPWD;
String strDBDriver,strURL;
String strSQL,strTemp;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
int iExist=0;//0:not exits,1:exits
HttpSession session;
session=req.getSession();
out = resp.getWriter();
//get parameters
strUserName=req.getParameter("username");
strPWD=req.getParameter("password");
//set database connection parameter value
strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
strURL="jdbc:odbc:wygl";
if(strUserName!=null || strUserName.compareTo("")!=0){
//instance session variable:
session.putValue("login_name","");
session.putValue("user_name","");
session.putValue("dept_id","");
session.putValue("duty","");
session.putValue("mright","");
session.putValue("uitype","");
session.putValue("logined","false");
session.putValue("info","null");
//connect database
try{
Class.forName(strDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println("error:"+e.getMessage());
}
try{
conn=DriverManager.getConnection(strURL);
stmt=conn.createStatement();
}catch(SQLException e){
System.out.println("error:"+e.getMessage());
}
strSQL="select * from sys_user where login_name='"+strUserName+"' and user_pwd='"+strPWD+"'";
try{
rs=stmt.executeQuery(strSQL);
if(rs.next()){
session.putValue("login_name",strUserName);
session.putValue("logined","true");
strTemp=rs.getString("user_name");
session.putValue("user_name",strTemp);
strTemp=rs.getString("dept_id");
session.putValue("dept_id",strTemp);
strTemp=rs.getString("duty");
session.putValue("duty",strTemp);
strTemp=rs.getString("mright");
session.putValue("mright",strTemp);
strTemp=rs.getString("uitype");
session.putValue("uitype",strTemp);
resp.sendRedirect("/wygl/jsp/sysindex.jsp");
}else{
resp.sendRedirect("/wygl/jsp/errorindex.jsp");
}
}catch(SQLException e){
System.err.println("error:"+e.getMessage());
}
}
}
}
|
conn.jsp
query.jsp
0)
{
String sql=" state='我不傻'";
int count=0;
try {
rcount = stmt.executeQuery("SELECT count(id) as id from user where "+sql);
catch(SQLException ex) {
out.print("aq.executeQuery: " + ex.getMessage());
}
if(rcount.next())
count = rcount.getInt("id");
rcount.close();
if (count>0)
{
sql="select * from user where "+sql;
try {
result = stmt.executeQuery(sql);
}
catch(SQLException ex) {
out.print("aq.executeQuery: " + ex.getMessage());
}
int i;
String name;
// result.first();
// result.absolute((pages-1)*pagesize);
// 此方法jdbc2.0支持。编译通过,但执行不过,不知是不是跟驱动有关,只好用下面的笨办法。
for(i=1;i
query.jsp
0)
{
String sql=" state='我不傻'";
int count=0;
try {
rcount = stmt.executeQuery("SELECT count(id) as id from user where "+sql);
catch(SQLException ex) {
out.print("aq.executeQuery: " + ex.getMessage());
}
if(rcount.next())
count = rcount.getInt("id");
rcount.close();
if (count>0)
{
sql="select * from user where "+sql;
try {
result = stmt.executeQuery(sql);
}
catch(SQLException ex) {
out.print("aq.executeQuery: " + ex.getMessage());
}
int i;
String name;
// result.first();
// result.absolute((pages-1)*pagesize);
// 此方法jdbc2.0支持。编译通过,但执行不过,不知是不是跟驱动有关,只好用下面的笨办法。
for(i=1;i