20201104陈柏安

对数据库的增删改查。
param 被执行的sql语句,只能执行insert、update、delete 语句
throws Exception
获取数据库连接getConnection();

获取sql的执行对象
stat = conn.createStatement();
执行sql
stat.executeUpdate(sql);

新建web数据库,关注两个文件夹,java resers和webcontent。在src里面建包,com.laobing.dbhamper
包里建类,new class,dbhamper 复制前一天的代码到这里,将上一周制作的界面拷过来。放在eclipse的webcontent文件夹里

executeQuery返回值类型是对表的一行,查询的返回值的结果就是结果集,用循环来for知道具体个数。
rs.next下一行
while(rs.next() )f
System. out. println(rs. getObject( "userNAME"));
System. out . println(rs. get0bject("gender"));
prepareStatement. close();
connection. close( ) 查询的方法,返回的结果值。

搭建环境变量:
新建一个文件-new-other-web-dynanic web project-shopping-next-next-对号-finish
java resources-src-右键-new-package-com.itlaobing.dbhelper-右键-class-DBHelper
java resources和webcontent中复制HTML中的文件的复制到esp中,及将文件放到javaresource
content-other-jsp-file-index.jsp-finish
window-最后-server-runtime-add-apache9.0-找目录文件的路径finish-apply and close
buid path-configur-lib-addlib-server runtime-选中Apache9.0-apply and close
server-右键-and remove-shopping
window-preperences-web-jsp files-utf_8-apply and close
window-preperences-other-utf_8
将html改为jspwindow-preperences-server-runtime environments-add-9.0-next-路径-finish-apply and close
package com. laobing. dbh;
import java. sq1. Connection;·
public class DBhelper {
/lmain28l/
public static void main(String[] args) {try {
save();
}
catch (Exception e) {
e. printStackTrace();
}
! eaaa2288028kE2 1
public static void save() throws Exception {
1 /019000000100000
C1ass. forName("com. mysq1.jdbc.Driver");
1 120000000890000
Connection connection = DriverManager . getConnection("jdbc :mysql://localhost:3306/db_ user", "root","");
1/300yAsq1900
String sql = "SELECT userNAME , gender FROM t
user";
1140000391
PreparedStatement prepareStatement = connection. prepareStatement(sql);
1/50000sq1
ResultSet rs=prepareStatement . executeQuery();
while(rs.next() )f
System. out. println(rs. getObject( "userNAME"));
System. out . println(rs. get0bject("gender"));
prepareStatement. close();
connection. close( );

心得体会:学习之后觉得数据库果然很厉害,这样的知识确实需要好好掌握。

标签

评论

© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1