javaBean封装数据库的访问
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
111111111111111111111111111111111111111111111111111111111111111 package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class db {
String url="jdbc:mysql://localhost/liuyan?useUnicode=true&characterEncoding=gb2312";
String username="root";
String password="lovebin";
Connection con=null;
Statement stmt;
ResultSet rs;
public db(){
stmt=null;
rs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("load Driver exception");
}
try{
con=DriverManager.getConnection(url, username, password);
}catch(SQLException e){
System.out.println("SQL exception");
}
if(con!=null)
{
System.out.println("lianjiechenggong");
}
}
public boolean exeSql(String strSql)
{
try{
stmt=con.createStatement();
stmt.executeUpdate(strSql);
return true;
}catch(Exception ex)
{
System.out.println(ex.toString());
return false;
}
}
public ResultSet exeSqlQuery(String strSql)
{
try{
stmt=con.createStatement();
rs=stmt.executeQuery(strSql);
}catch(Exception ex)
{
System.out.println(ex.toString());
rs=null;
}
return rs;
}
}
2222222222222222222222222222222222222222222222222222222222222222 package com;
import java.sql.*;
import java.text.*;
public class users extends db{
private int UserID;
private String UserName;
private String UserPassword;
private String strSql;
public users()
{
UserID=0;
UserName="";
UserPassword="";
strSql="";
}
public boolean add_user()
{
strSql="insert into users(UserName,UserPassword)";
strSql=strSql+"values(";
strSql=strSql+"'"+UserName+"',";
strSql=strSql+"'"+UserPassword+"'";
strSql=strSql+")";
boolean isAdd=super.exeSql(strSql);
return isAdd;
}
public boolean is_exist()
{
strSql="select * from 'users'where UserName=UserName";
ResultSet rs=null;
boolean isExist=false;
try{
rs=super.exeSqlQuery(strSql);
while(rs.next())
{
isExist=true;
}
}catch(Exception ex)
{
System.out.println(ex.toString());
}
return isExist;
}
public boolean user_valid()
{
strSql="select * from users where UserName=" +
"'"+UserName+"'" +
"and UserPassword=" +
"'"+UserPassword+"'";
ResultSet rs=null;
boolean isvalid=false;
try{
rs=super.exeSqlQuery(strSql);
while(rs.next())
{
erID=rs.getInt("UserID");
isvalid=true;
}
}catch(Exception ex)
{
System.out.println(ex.toString());
}
return isvalid;
}
public boolean init()
{
strSql="select * from users where UserID= ";
strSql=strSql+UserID;
try{