java连接数据库jdbc基础

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1表createtablet_user(usernamevarchar2(20)notnullprimarykey,pwdvarchar2(20)notnull,statusvarchar2(20)notnull,qqvarchar2(20)notnull,registtimevarchar2(20)notnull);select*fromt_user;2基本的数据库连接packagecom.{publicstaticfinalStringDRIVER=oracle.jdbc.driver.OracleDriver;publicstaticfinalStringURL=jdbc:oracle:thin:@localhost:1521:XE;publicstaticfinalStringUSER=SCOTT;publicstaticfinalStringPASSWORLD=SCOTT;publicstaticvoidmain(String[]args){Connectionconn=null;try{Class.forName(DRIVER);conn=DriverManager.getConnection(URL,USER,PASSWORLD);System.out.println(conn);System.out.println(链接成功);}catch(Exceptione){//TODO:handleexceptione.printStackTrace();}}}3JDBC进行查询packagecom.{privatestaticvoidtestSelect(Stringstatus){Connectionconn=null;java.sql.Statementstmt=null;try{Class.forName(oracle.jdbc.driver.OracleDriver);conn=DriverManager.getConnection(jdbc:oracle:thin:@localhost:1521:XE,SCOTT,SCOTT);stmt=conn.createStatement();Stringsql=select*fromT_USERwherestatus=+status;ResultSetrs=((java.sql.Statement)stmt).executeQuery(sql);//只有查询有结果集while(rs.next()){System.out.println(rs.getString(1)++rs.getString(2)++rs.getString(3)++rs.getString(4)++rs.getString(5));}}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{if(stmt!=null){try{((Connection)stmt).close();}catch(SQLExceptione){e.printStackTrace();}}if(conn!=null){try{conn.close();}catch(SQLExceptione){e.printStackTrace();}}}}publicstaticvoidmain(String[]args){/*testInsert();*//*testInsert(图图,1,1,3,123);*/testSelect(2);}}4数据库插入数据packagecom.{/*添加数据库*/publicstaticvoidtestInsert(){Connectionconn=null;java.sql.Statementstmt=null;try{Class.forName(oracle.jdbc.driver.OracleDriver);conn=DriverManager.getConnection(jdbc:oracle:thin:@localhost:1521:XE,SCOTT,SCOTT);stmt=conn.createStatement();Stringsql=insertintot_uservalues('tutu7','1','1','5213213','15215132');stmt.executeUpdate(sql);}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{if(stmt!=null){try{((Connection)stmt).close();}catch(SQLExceptione){e.printStackTrace();}}if(conn!=null){try{conn.close();}catch(SQLExceptione){e.printStackTrace();}}}}publicstaticvoidmain(String[]args){testInsert();/*testInsert(图图5,1,0,321,12312414);*//*testSelect(2);*/}}5预编译插入数据packagecom.{/*预编译插入数据*/publicstaticvoidtestInsert(Stringusername,Stringpwd,Stringstatus,Stringqq,Stringregisttime){Connectionconn=null;PreparedStatementpstmt=null;try{Class.forName(oracle.jdbc.driver.OracleDriver);conn=DriverManager.getConnection(jdbc:oracle:thin:@localhost:1521:XE,SCOTT,SCOTT);Stringsql=insertintot_uservalues(?,?,?,?,?);pstmt=conn.prepareStatement(sql);pstmt.setString(1,username);pstmt.setString(2,pwd);pstmt.setString(3,status);pstmt.setString(4,qq);pstmt.setString(5,registtime);pstmt.executeUpdate();}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{if(pstmt!=null){try{((Connection)pstmt).close();}catch(SQLExceptione){e.printStackTrace();}}if(conn!=null){try{conn.close();}catch(SQLExceptione){e.printStackTrace();}}}}publicstaticvoidmain(String[]args){/*testInsert();*/testInsert(图图5,1,0,321,12312414);/*testSelect(2);*/}}6三大框架下使用数据库登录User.javapublicclassUser{privateStringusername;privateStringpwd;privateStringstatus;privateStringqq;privateStringregisttime;publicUser(){super();//TODOAuto-generatedconstructorstub}publicUser(Stringusername,Stringpwd,Stringstatus,Stringqq,Stringregisttime){super();this.username=username;this.pwd=pwd;this.status=status;this.qq=qq;this.registtime=registtime;}UserDao.javapackagecom.{privateConnectionconn=ConnectionFactory.getConnection();publicUserselectByNamePwd(Stringusername,Stringpwd){Useruser=null;Stringsql=select*fromUserwhereusername=?andpwd=?;PreparedStatementpstmt=null;ResultSetrs=null;try{pstmt=conn.prepareStatement(sql);pstmt.setString(1,username);pstmt.setString(2,pwd);pstmt.setString(3,status);pstmt.setString(4,qq);pstmt.setString(5,registtime);rs=pstmt.executeQuery();if(rs.next()){user=newUser(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));}}catch(Exceptione){e.printStackTrace();}finally{if(pstmt!=null){try{((Connection)pstmt).close();}catch(SQLExceptione){e.printStackTrace();}}if(conn!=null){try{conn

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功