1、精品文档/用户登陆数据库代码:importjava.sql.*;classDatabaseConnectioncon;ResultSetrs;Statementstmt;publicDatabase()tryClass.forName(sun.jdbc.odbc.JdbcOdbcDriver);/加载JDBC-ODBC桥驱动程序Stringurl=jdbcdbc:HDB;con=DriverManager.getConnection(url);/连接数据库HDB/stmt提供一个创建SQL查询、执行查询、得到返回结果的空间stmt=/con.createStatement(ResultSet.
2、TYPE_SCROLL_SENSITIVE,/ResultSet.CONCUR_READ_ONLY);catch(Exceptionex)System.out.println(ex);/*根据用户名,获取密码*/publicStringgetPswd(Stringname)try/rs为执行SQL语句所查询的结果赋给结果集对象rsrs=stmt.executeQuery(SELECTpswdFROMUserWHEREName=+name+);rs.last();/把rs的指针移到最后一行if(rs.getRow()=0)returnnull;elseStringpswdDB=rs.getStr
3、ing(Pswd);/获取rs结果集中的pswd列的数据returnpswdDB;catch(Exceptione)System.out.println(e);returnnull;/*增加一行用户名、密码数据*/publicbooleaninsertData(Stringname,Stringpswd)tryStrings=getPswd(name);if(s=null)intrtn=stmt.executeUpdate(INSERTINTOUserVALUES(+name+,+pswd+);if(rtn!=0)returntrue;elsereturnfalse;returnfalse;c
4、atch(Exceptionet)System.out.println(et);returnfalse;/进库数据库代码:importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;classBookDatabaseConnectioncon;ResultSetrs;Statementstmt;publicBookDatabase()tryClass.forName(sun.jdbc.odbc.JdbcOdbcDriver);/加载JDBC-OD
5、BC桥驱动程序Stringurl=jdbcdbc:Book;con=DriverManager.getConnection(url);/连接数据库HDB/stmt提供一个创建SQL查询、执行查询、得到返回结果的空间stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);catch(Exceptionex)System.out.println(ex);/*增加一行用户名、密码数据*/publicbooleaninsertData(Stringid,Stringname,Stringw
6、here,Stringprice,Stringdate)tryintrtn=stmt.executeUpdate(INSERTINTOBookVALUES(+id+,+name+,+where+,+price+,+date+);if(rtn!=0)returntrue;elsereturnfalse;catch(Exceptionet)System.out.println(et);returnfalse;/*publicbooleandeleteData(Stringid)tryintrtn=*stmt.executeUpdate(DELETEFROMBookWHEREid=001);if(r
7、tn!=0)*returntrue;elsereturnfalse;catch(Exceptione)*System.out.println(e);returnfalse;publicbooleanSelect(String*id)tryintrtn=stmt.executeUpdate(SELECT*FROMBookInWHERE*id=+id+);*if(rtn!=0)returntrue;elsereturnfalse;catch(Exception*e)System.out.println(e);returnfalse;*/出库数据库代码:importjava.sql.Connecti
8、on;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;classBookDatabase1Connectioncon;ResultSetrs;Statementstmt;publicBookDatabase1()tryClass.forName(sun.jdbc.odbc.JdbcOdbcDriver);/加载JDBC-ODBC桥驱动程序Stringurl=jdbcdbc:Book;con=DriverManager.getConnection(url);/连接数据库HDB/stmt提
9、供一个创建SQL查询、执行查询、得到返回结果的空间stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);catch(Exceptionex)System.out.println(ex);/*增加一行用户名、密码数据publicbooleaninsertData(Stringid,Stringname,String*where,String*price,Stringdate)tryintrtn=stmt.executeUpdate(INSERTINTO*BookInVALUES(+
10、id+*,+name+,+where+,+price+,+date+);*if(rtn!=0)returntrue;elsereturnfalse;catch(Exception*et)System.out.println(et);returnfalse;*/publicbooleandeleteData(Stringid)tryintrtn=stmt.executeUpdate(DELETEFROMBookWHEREid=+id+);if(rtn!=0)returntrue;elsereturnfalse;catch(Exceptione)System.out.println(e);retu
11、rnfalse;/*publicbooleanSelect(Stringid)tryintrtn=*stmt.executeUpdate(SELECT*FROMBookInWHEREid=+id+);if(rtn!=*0)returntrue;elsereturnfalse;catch(Exceptione)*System.out.println(e);returnfalse;*/查询数据库代码;importjava.awt.*;importjava.awt.event.*;importjava.sql.*;importjavax.swing.*;classBookDatabase2exten
12、dsJFrameStringnumber,name,address,prices,day;Connectioncon;Statementstmt=null;ResultSetresult=null;Stringsql=null;publicBookDatabase2()tryClass.forName(sun.jdbc.odbc.JdbcOdbcDriver);/加载JDBC-ODBC桥驱动程序Stringurl=jdbc:odbc:Book;con=DriverManager.getConnection(url);/连接数据库HDB/stmt提供一个创建SQL查询、执行查询、得到返回结果的空
13、间stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);catch(Exceptionex)System.out.println(ex);publicvoidselect(Stringid)trysql=SELECT*FROMBookwhereid=+id+;result=stmt.executeQuery(sql);while(result.next()number=result.getString(1);name=result.getString(2);address=result.getString(3);prices=result.getString(4);day=result.getString(4);JOptionPane.showMessageDialog(BookDatabase2.this,成功查询图书!n+图书ID:+number+n+图书名称:+name+n+出版社+address+n+价格:+prices+n+出版日期:+day+n,提示,JOptionPane.ERROR_MESSAGE);catch(Exceptione)System.out.println(e);可修改
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100