2023年8月3日发(作者:)
Java在应用Oracle中的基础
数据列表封装
publicclass User {
private String name;
privateintpassword;
public String getName() {
returnname;
}
publicvoidsetName(String name) {
= name;
}
publicintgetPassword() {
returnpassword;
}
publicvoidsetPassword(intpassword) {
rd = password;
}
}
数据库连接
tion;
Manager;
eption;
ent;
publicclass Base {
public Connection getConnection (){
Connection conn=null;
//声明
try{
e("Driver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String uName="scott";
String password="tiger";
conn=nection(url, uName, password);
}
}
}
catch(Exception e){
n("数据库连接有异常");
}
returnconn;
}
publicvoid update(String sql){
Connection conn=null;
Statement state=null;
try{
conn=getConnection();
state=Statement();
intm=eUpdate(sql);
n("本次更新了"+m+"条数据");
}
catch(Exception e){
n("修改数据有异常");
}
finally{
closeBase(conn,state);
}
}
publicvoidcloseBase(Connection conn ,Statement state){
try {
if(state!=null){
();
if(conn!=null){
();
}
}
}
catch (SQLExceptione) {
// TODO Auto-generated catch block
tackTrace();
}
数据库方法封装
publicclass Fang extends Base{
publicvoidzeng(intid,Stringname,intxingbie,longtel){
String sql="insert into u_user"
+ " values("+id+",'"+name+"',"+xingbie+","+tel+ ")";
(sql);
}
publicvoidshan(intid){
String sql="delete from u_user where id="+id;
(sql);
}
publicvoidgai(intid,Stringname,intxingbie,longtel){
String sql="insert into u_user"
+ " values("+id+",'"+name+"',"+xingbie+","+tel+ ")";
(sql);
}
publicvoid cha(intid,Stringname,intxingbie,longtel){
String sql="insert into u_user"
+ " values("+id+",'"+name+"',"+xingbie+","+tel+ ")";
}
}
数据库连接封装
tion;
Manager;
Set;
ent;
publicclassBaseDao {
public Connection getConnection()
{
Connection conn=null;
try {
//申明数据库驱动
e("Driver");
//
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String uname="scott";
String password="tiger";
conn=nection(url,uname,password);
} catch (Exception e) {
// TODO Auto-generated catch block
n("数据库连接异常");
}
returnconn;
}
}
publicResultSet Query(String sql){
Connection conn=null;
Statement statm=null;
ResultSetrs=null;
try{
conn=getConnection();
statm=Statement();
rs=eQuery(sql);
}catch(Exception e){
n("执行时出现错误");
}
returnrs;
}
publicvoid update(String sql){
Connection conn=null;
Statement state=null;
try{
conn=getConnection();
state=Statement();
intm=eUpdate(sql);
n("本次更新了"+m+"条数据!");
}
catch (Exception e ){
n("修改数据有异常");
}
}
publicvoidcloseAll(ResultSetrs,Connectionconn){
try{if(rs!=null){
();
}
if(conn!=null){
();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
n("数据库关闭异常");
}
} 数据库方法封装
tion;
Set;
eption;
ent;
;
publicclassLoginDaoextendsBaseDao {
public User login(String name,Stringpassword){
String sql="select * from sbs_user"
+ " where name='"+name+"'and password='"+password+"'";
User user=null;
Connection conn=null;
Statement state=null;
ResultSetrs=null;
conn=nection();
try {
state=Statement();
rs=eQuery(sql);
while(()){
user=new User();
e(ing("name"));
sword(("password"));
}
closeAll(rs,conn);
} catch (SQLExceptione) {
// TODO Auto-generated catch block
tackTrace();
}
returnuser;
}
}
HTML应用数据库的封装
ption;
tException;
rvlet;
rvletRequest;
rvletResponse;
ao; ;
publicclassLoginServletextendsHttpServlet {
@Override
protectedvoiddoGet(HttpServletRequestreq, HttpServletResponseresp)
throwsServletException, IOException {
String name=ameter("name");
String password=ameter("password");
LoginDaologin=newLoginDao ();
User user=new User();
user=(name,password);
if(user!=null)
{
ribute("name", name);
ribute("password", password);
//成功
uestDispatcher("admin_").forward(req,
resp);
}else{
//失败
uestDispatcher("").forward(req,
resp);
}
}
@Override
protectedvoiddoPost(HttpServletRequestreq,
HttpServletResponseresp) throwsServletException, IOException {
// TODO Auto-generated method stub
(req, resp);
}
}
配置HTML
/xml/ns/javaee/web-app_3_"id="WebApp_ID"version="3.1">
2023年8月3日发(作者:)
Java在应用Oracle中的基础
数据列表封装
publicclass User {
private String name;
privateintpassword;
public String getName() {
returnname;
}
publicvoidsetName(String name) {
= name;
}
publicintgetPassword() {
returnpassword;
}
publicvoidsetPassword(intpassword) {
rd = password;
}
}
数据库连接
tion;
Manager;
eption;
ent;
publicclass Base {
public Connection getConnection (){
Connection conn=null;
//声明
try{
e("Driver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String uName="scott";
String password="tiger";
conn=nection(url, uName, password);
}
}
}
catch(Exception e){
n("数据库连接有异常");
}
returnconn;
}
publicvoid update(String sql){
Connection conn=null;
Statement state=null;
try{
conn=getConnection();
state=Statement();
intm=eUpdate(sql);
n("本次更新了"+m+"条数据");
}
catch(Exception e){
n("修改数据有异常");
}
finally{
closeBase(conn,state);
}
}
publicvoidcloseBase(Connection conn ,Statement state){
try {
if(state!=null){
();
if(conn!=null){
();
}
}
}
catch (SQLExceptione) {
// TODO Auto-generated catch block
tackTrace();
}
数据库方法封装
publicclass Fang extends Base{
publicvoidzeng(intid,Stringname,intxingbie,longtel){
String sql="insert into u_user"
+ " values("+id+",'"+name+"',"+xingbie+","+tel+ ")";
(sql);
}
publicvoidshan(intid){
String sql="delete from u_user where id="+id;
(sql);
}
publicvoidgai(intid,Stringname,intxingbie,longtel){
String sql="insert into u_user"
+ " values("+id+",'"+name+"',"+xingbie+","+tel+ ")";
(sql);
}
publicvoid cha(intid,Stringname,intxingbie,longtel){
String sql="insert into u_user"
+ " values("+id+",'"+name+"',"+xingbie+","+tel+ ")";
}
}
数据库连接封装
tion;
Manager;
Set;
ent;
publicclassBaseDao {
public Connection getConnection()
{
Connection conn=null;
try {
//申明数据库驱动
e("Driver");
//
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String uname="scott";
String password="tiger";
conn=nection(url,uname,password);
} catch (Exception e) {
// TODO Auto-generated catch block
n("数据库连接异常");
}
returnconn;
}
}
publicResultSet Query(String sql){
Connection conn=null;
Statement statm=null;
ResultSetrs=null;
try{
conn=getConnection();
statm=Statement();
rs=eQuery(sql);
}catch(Exception e){
n("执行时出现错误");
}
returnrs;
}
publicvoid update(String sql){
Connection conn=null;
Statement state=null;
try{
conn=getConnection();
state=Statement();
intm=eUpdate(sql);
n("本次更新了"+m+"条数据!");
}
catch (Exception e ){
n("修改数据有异常");
}
}
publicvoidcloseAll(ResultSetrs,Connectionconn){
try{if(rs!=null){
();
}
if(conn!=null){
();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
n("数据库关闭异常");
}
} 数据库方法封装
tion;
Set;
eption;
ent;
;
publicclassLoginDaoextendsBaseDao {
public User login(String name,Stringpassword){
String sql="select * from sbs_user"
+ " where name='"+name+"'and password='"+password+"'";
User user=null;
Connection conn=null;
Statement state=null;
ResultSetrs=null;
conn=nection();
try {
state=Statement();
rs=eQuery(sql);
while(()){
user=new User();
e(ing("name"));
sword(("password"));
}
closeAll(rs,conn);
} catch (SQLExceptione) {
// TODO Auto-generated catch block
tackTrace();
}
returnuser;
}
}
HTML应用数据库的封装
ption;
tException;
rvlet;
rvletRequest;
rvletResponse;
ao; ;
publicclassLoginServletextendsHttpServlet {
@Override
protectedvoiddoGet(HttpServletRequestreq, HttpServletResponseresp)
throwsServletException, IOException {
String name=ameter("name");
String password=ameter("password");
LoginDaologin=newLoginDao ();
User user=new User();
user=(name,password);
if(user!=null)
{
ribute("name", name);
ribute("password", password);
//成功
uestDispatcher("admin_").forward(req,
resp);
}else{
//失败
uestDispatcher("").forward(req,
resp);
}
}
@Override
protectedvoiddoPost(HttpServletRequestreq,
HttpServletResponseresp) throwsServletException, IOException {
// TODO Auto-generated method stub
(req, resp);
}
}
配置HTML
/xml/ns/javaee/web-app_3_"id="WebApp_ID"version="3.1">
发布评论