资源描述:
《单表增删改查.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、单表增删改查第一步:创建数据库以数据库表createdatabaseusersgouseusersgocreatetableusers(uidintnotnullprimarykeyidentity(1,1),unamevarchar(50)notnullunique,usexvarchar(10)notnull,regTimedatetimedefaultgetDate())goinsertintousers(uname,usex)values('linder','男')insertintousers(una
2、me,usex)values('jack','男')insertintousers(uname,usex)values('tom','男')insertintousers(uname,usex)values('rose','女')insertintousers(uname,usex)values('jhon','男')insertintousers(uname,usex)values('pack','男')insertintousers(uname,usex)values('mary','女')goselect
3、*fromusersgo第二步:写实体packagenet.xabc.users.entity;publicclassUsers{privateintuid;privateStringuname;privateStringusex;privateStringregTime;publicStringgetRegTime(){returnregTime;}publicvoidsetRegTime(StringregTime){this.regTime=regTime;}publicintgetUid(){retur
4、nuid;}publicvoidsetUid(intuid){this.uid=uid;}publicStringgetUname(){returnuname;}publicvoidsetUname(Stringuname){this.uname=uname;}publicStringgetUsex(){returnusex;}publicvoidsetUsex(Stringusex){this.usex=usex;}}第三步:写数据库连接类BaseDaopackagenet.xabc.users.db;imp
5、ortjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;publicclassBaseDao{privatestaticfinalStringDRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";privatestaticfinalStringURL="jdbc:sqlserver://local
6、host:1434;DatabaseName=users";privatestaticfinalStringUSERS="sa";privatestaticfinalStringPWD="linder";privateConnectionconn=null;/***获取数据库连接**/publicConnectiongetConnection(){try{//加载驱动Class.forName(DRIVER);//获取数据库连接conn=DriverManager.getConnection(URL,USERS
7、,PWD);returnconn;}catch(Exceptione){System.out.println("获取数据库连接异常:"+e.getMessage());returnnull;}}/***释放资源**/publicvoidcloseAll(Connectionconn,PreparedStatementpstmt,ResultSetres){this.closeResultSet(res);this.closePreparedStatement(pstmt);this.closeConnectio
8、n(conn);}privatevoidcloseConnection(Connectionconn){try{if(!conn.isClosed()&&conn!=null){conn.close();conn=null;}}catch(Exceptione){System.out.println("关闭数据库连接异常:"+e.getMessage());}}privatevoidc