基础day17导jar包驱动_第1页
基础day17导jar包驱动_第2页
基础day17导jar包驱动_第3页
基础day17导jar包驱动_第4页
基础day17导jar包驱动_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JDBCjarJDBCSUNAPI,但是当他们刚刚开始时就SUN提供一套访问数据库的规范(就是一组接口,并提供连接数据库的API被称之为驱动!JDBCJDBC驱动才是接口的实现,没有驱动无法完成数据库连接!每个数据库厂商结果就是ResultSet;ResultSet对象表示查询结果集,只有在执行查询操作后才会有结果集的产生。结果集是一个二Hello获取连接需要两步,一是使用DriverManager来注册驱动,二是使用DriverManager来看看com.mysql.jdbc.Driver类的源代码:statictry}catch(SQLExceptionE)thrownewRuntimeException("Can'tregister}}}com.mysql.jdbc.Driverstaticcom.mysql.jdbc.Driver注册到DriverManager中,所以可以把注册驱动类的代码修改为加载驱动类。获取连接的也只有一句代码:DriverManager.getConnection(url,username,password),其中需要提供一个url。下面是mysql的url:第三部分是由数据库厂商规定的,我们需要了解每个数据库厂商的要求,mysql的第三部分(localhost(3306Connectioncon=还可以在url中提供参数:characherEncodingUTF-8编码。请注意,mysql中指定UTF-8编码是给出的是UTF8,而不是UTF-8。要小心了!Statementstmt=Stringsql=“insertintouservalue(’zhangSan’,intm=能影响一行,而updatedelete可能会影响0~n行。Stringsql=“select*fromResultSetrs=方法,而是rs.getObject(1)ResultSetgetXXX()方法,比较常用的方法有:StringgetString(intcol)intgetInt(intcol)doublegetDouble(intpublicstaticConnectiongetConnection()throws{Stringurl=returnDriverManager.getConnection(url,"root",}publicvoidinsert()throws{Connectioncon=getConnection();Statementstmt=con.createStatement();Stringsql="insertintouservalues('zhangSan','123')";}publicvoidupdate()throws{Connectioncon=getConnection();Statementstmt=con.createStatement();}publicvoiddelete()throws{Connectioncon=getConnection();Statementstmt=con.createStatement();Stringsql="deletefromuserwhereusername='zhangSan'";}publicvoidquery()throws{Connectioncon=getConnection();Statementstmt=con.createStatement();Stringsql="select*fromuser";ResultSetrs=stmt.executeQuery(sql);while(rs.next()){Stringusername=rs.getString(1);Stringpassword=System.out.println(username+","+}}果你还记得IO流的规范化代码,那么下面的代码你就明白什么意思了。publicvoid{Connectioncon=null;Statementstmt=null;ResultSetrs=null;try{con=stmt=con.createStatement();Stringsql="select*fromuser";rs=stmt.executeQuery(sql);while(rs.next()){Stringusername=rs.getString(1);Stringpassword=System.out.println(username+","+}}catch(Exceptione)thrownew}finallytryif(rs!=null)rs.close();if(stmt!=null)stmt.close();if(con!=null)con.close();}catch(SQLExceptione)}}Stringurl=Stringusername=Stringpassword=Statementstmt=Statementstmt=intexecuteUpdate(Stringsql)insert、update、delete语句,其实这ResultSetexecuteQuery(Stringsql)ResultSet,即结果boolean句。该方法返回的是booleanSQL语句是否有结果集!。execute()intgetUpdateCount()insert、update、delete语句所影响的行数。下N行上N行ResultSe示向上移动row行;获取结果集列数:int获取指定列的列名:StringgetColumnName(int结果集是否支持滚动,要从Connection类的createStatement()方法说起。也就是说创建的Statement决定了使用Statement创建的ResultSet是否支持滚动。resultSetType的可选值:Connectioncon=Stringsql=next()ResultSet的游标向下移动,当游标移动到你需要的行时,就需要来获取该行的数据了,ResultSet提供了一系列的获取列数据的方法:SQLuidCHAR(32)PRIMARY INSERTINTOuserVALUES('U_1001','zs',SELECT*FROM下面我们写一个login()方法!publicpublicvoidlogin(Stringusername,String{Connectioncon=null;Statementstmt=null;ResultSetrs=null;try{con=JdbcUtils.getConnection();stmt=con.createStatement();Stringsql="SELECT*FROMuserWHERE"+"username='"+username+"'andpassword='"+password+"'";rs=stmt.executeQuery(sql);if(rs.next())System.out.println("欢迎}else}}catch(Exceptione)thrownew}finallyJdbcUtils.close(con,stmt,}}login("a'login("a'or'a'='a","a'orPreparedStatement的好处:StringStringsql=“select*fromtab_studentwheres_number=?”;PreparedStatementpstmt=con.prepareStatement(sql);pstmt.setString(1,“S_1001”);rs=pstmt.executeQuery();“?”的SQL语句,其中“?”就是参数。注意PreparedStatement对象独有的executeQuery()方法是没有参数的,而Statement的executeQuery()是需要参数(SQL语句)PreparedStatement对象时已经让它与一条PreparedStatement,而不是使用JdbcUtils写一个JdbcUtils类,让它从配置文件中读取配置参数,然后创建连接对象。publicpublicclassJdbcUtilsprivatestaticfinalStringdbconfig=privatestaticPropertiesprop=newstatictryInputStreamin=}catch(IOExceptione)thrownew}}publicstaticConnectiongetConnection()tryreturn}catch(Exceptione)thrownew}}}publicpublicclassUserprivateStringuid;privateStringusername;privateString…}publicpublicinterface{publicvoidadd(Useruser);publicvoidmod(Useruser);publicvoiddel(Stringuid);publicUserload(Stringuid);publicList<User>findAll();publicclassUserDaoImplimplementsUserDaopublicvoidadd(User{Connectioncon=null;PreparedStatementpstmt=null;try{con=Stringsql="insertintouservalue(?,?,?)";pstmt=con.prepareStatement(sql);pstmt.setString(1,user.getUid());pstmt.setString(2,user.getUsername());pstmt.setString(3,user.getPassword());}catch(Exceptione)thrownew}finallytryif(pstmt!=null)if(con!=null)}catch(SQLExceptione)}}publicvoidmod(User{Connectioncon=null;PreparedStatementpstmt=null;try{con=Stringsql="updateusersetusername=?,password=?whereuid=?";pstmt=con.prepareStatement(sql);pstmt.setString(3,user.getUid());publicvoiddel(String{Connectioncon=null;PreparedStatementpstmt=null;try{con=Stringsql="deletefromuserwhereuid=?";pstmt=con.prepareStatement(sql);pstmt.setString(1,uid);}catch(Exceptione)thrownew}finallytryif(pstmt!=null)if(con!=null)}catch(SQLExceptione)}}publicUserload(String{Connectioncon=null;PreparedStatementpstmt=null;ResultSetrs=null;trycon=Stringsql="select*fromuserwhereuid=?";pstmt=con.prepareStatement(sql);pstmt.setString(1,uid);rs=if(rs.next())returnnewUser(rs.getString(1),rs.getString(2),}returnpublicpublicList<User>{Connectioncon=null;PreparedStatementpstmt=null;ResultSetrs=null;trycon=JdbcUtils.getConnection();Stringsql="select*fromuser";pstmt=con.prepareStatement(sql);rs=pstmt.executeQuery();List<User>userList=newuserList.add(newUser(rs.getString(1),rs.getString(2),}return}catch(Exceptione)thrownew}finallytryif(pstmt!=null)if(con!=null)}catch(SQLExceptione)}}}publicpublicclass{privatestaticuserDao;statictryInputStreamin=Propertiesprop=newProperties();StringclassName=prop.getProperty("cn.itcast.jdbc.UserDao");Classclazz=Class.forName(className);userDao=(UserDao)}catch(Exceptione)thrownew}}publicstaticUserDaogetUserDao()returnreturn}}DATE→java.sql.DateTIME→TIMESTAMP→,→utilDatejava.sql.Date、Time、Timestampjava.util.Datedate=newjava.util.Date();longl=java.sql.DatesqlDate=new这三个类都是java.util.Date的子类。java.sql.Datedate=java.sql.Timetime=java.util.Dated=java.util.Date转换成数据库的三种时间类型时,这就不能直接赋值了,这需要使用数类型的参数,表示毫秒值。创建这三个类型的对象,只需要有毫秒值即可。我们知道java.util.DategetTime()方法可以获取毫秒值,那么这个转换也就不是什么问题了。java.utl.Dated=newTimetime=newTime(d.getTime());//会丢失年月日Timestamptimestamp=newdt(dt(dDATE,tTIME,ts)publicvoidfun1()throws{Connectioncon=JdbcUtils.getConnection();Stringsql="insertintodtvalue(?,?,?)";PreparedStatementpstmt=con.prepareStatement(sql);java.util.Dated=newjava.util.Date();pstmt.setDate(1,newjava.sql.Date(d.getTime()));pstmt.setTime(2,newTime(d.getTime()));pstmt.setTimestamp(3,newTimestamp(d.getTime()));}}publicvoidpublicvoidfun2()throws{Connectioncon=JdbcUtils.getConnection();Stringsql="select*fromdt";PreparedStatementpstmt=con.prepareStatement(sql);ResultSetrs=pstmt.executeQuery();java.util.Dated1=rs.getDate(1);java.util.Dated2=rs.getTime(2);java.util.Dated3=rs.getTimestamp(3);}在my.ini中添加如下配置! INT INTPRIMARYKEYfilenameVARCHAR(100), con=Stringsqlcon=Stringsql="insertintotab_bin(filename,data)values(?,?)";pstmt=con.prepareStatement(sql);InputStreamin=newFileInputStream("f:\\a.jpg");pstmt.setBinaryStream(2,in);con=Stringsql="selectcon=Stringsql="selectfilename,datafromtab_binwhereid=?";pstmt=con.prepareStatement(sql);pstmt.setInt(1,rs=pstmt.executeQuery();Stringfilename=OutputStreamout=newFileOutputStream("F:\\"+InputStreamin=rs.getBinaryStream("data");IOUtils.copy(in,out);concon=concon=Stringsql="selectfilename,datafromtab_binwhereid=?";pstmt=con.prepareStatement(sql);pstmt.setInt(1,rs=pstmt.executeQuery();Stringfilename=rs.getString("filename");File

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论