当前位置: 首页 > news >正文

百度云网盘搜索引擎一个网站如何优化

百度云网盘搜索引擎,一个网站如何优化,找网站建设企业,上海 网站开发 工作室新建Maven项目 创建Maven项目 添加依赖 在pom.xml的标签里加上下面的内容 如果是MySQL 5.8那么的版本号是5.x.x, 例如5.1.49 如果是MySQL 8.0那么的版本号是8.x.x, 例如 8.0.28 dependencies!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java …新建Maven项目 创建Maven项目 添加依赖 在pom.xml的标签里加上下面的内容 如果是MySQL 5.8那么的版本号是5.x.x, 例如5.1.49 如果是MySQL 8.0那么的版本号是8.x.x, 例如 8.0.28 dependencies!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.49/version/dependency/dependencies新建实体类 构建实体类, 类属性和表对应上, 方便来接收查询数据 举例 对于一个学生信息表 CREATE TABLE student(id BIGINT,name VARCHAR(100),sn BIGINT,email VARCHAR(100),classId BIGINT)新建学生类 public class Student {private int id;private String name;private long sn;private String email;private int classId;//记得自己加上toString()方法,和每个属性的getter和Setter方法 }连接数据库查询内容 import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import javax.sql.DataSource; import java.sql.*; import java.util.Scanner;public class Demo01_Connection {//先定义数据源对象, 待会用这个数据源操作数据库//先定义一个名字, 待会再具体实现private static DataSource dataSource null;//定义自己数据库的各种属性, 方便全局修改// 数据库的用户名private static final String USER root;// 数据库的密码private static final String PASSWORD 123456;// 数据库连接字符串(针对MySQL5) 本机数据库ip就是localhost, 端口默认3306private static final String URL jdbc:mysql://数据库服务ip:端口号/数据库名?characterEncodingutf-8useSSLfalse;// 如果是MySQL 8则是下面(mysql 8要加时区设置)//private static final String URL jdbc:mysql://数据库服务ip:端口号/数据库名?characterEncodingutf8useSSLfalseserverTimezoneAsia/Shanghai;public static void main(String[] args) {// 1. 初化始数据源,待会赋给上面定义的数据源MysqlDataSource myDataSource new MysqlDataSource();// 2. 设置连接的参数myDataSource.setURL(URL);myDataSource.setUser(USER);myDataSource.setPassword(PASSWORD);// 3. 把构建好的Mysql数据源赋值给JDBC中的datasourcedataSource myDataSource;//下面三个声明在try{}之外,方便finally也能识别Connection connection null;PreparedStatement statement null;ResultSet resultSet null;try{//通过数据源获取一个数据库连接connection dataSource.getConnection();//接收用户输入的值System.out.print(请输入姓名:);Scanner scanner new Scanner(System.in);String sn scanner.next();//定义SQL语句String sql select * from student where name ?;// 获取一个预处理对象statement connection.prepareStatement(sql);// 处理占位符的值,要匹配上类型,参数1代表第几个占位符,后者表示要被替换的内容statement.setString(1, sn);// 4. 执行SQLresultSet statement.executeQuery();if (resultSet.next()) { //如果读取到了内容// 创建表示结果的JAVA对象Student student new Student();student.setId(resultSet.getInt(1));student.setName(resultSet.getString(2));student.setSn(resultSet.getLong(3));student.setEmail(resultSet.getString(4));student.setClassId(resultSet.getInt(5));System.out.println(student);}}catch (SQLException e){e.printStackTrace();}finally {// 依次关闭资源,从后往前关if (resultSet ! null) {try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if (statement ! null) {try {statement.close();} catch (SQLException e) {e.printStackTrace();}}if (connection ! null) {try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}} } 将一系列操作封装成工具类 import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import javax.sql.DataSource; import java.sql.*;public class DBUtil {// 先定义一个数据源对象private static DataSource dataSource null;// 数据库的用户名private static final String USER root;// 数据库的密码private static final String PASSWORD 123456;// 数据库连接字符串private static final String URL jdbc:mysql://127.0.0.1:3306/java78?characterEncodingutf-8useSSLfalse;static {// 初始化数据源,类加载时运行MysqlDataSource mysqlDataSource new MysqlDataSource();mysqlDataSource.setURL(URL);mysqlDataSource.setUser(USER);mysqlDataSource.setPassword(PASSWORD);dataSource mysqlDataSource;}// 用private 修饰构造方法使外部不能new 这个类的对象private DBUtil() {}//获取数据库连接public static Connection getConnection () throws SQLException {return dataSource.getConnection();}//关闭对象并释放资源public static void close (ResultSet resultSet, PreparedStatement statement, Connection connection) {// 依次关闭对象并释放资源if (resultSet ! null) {try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if (statement ! null) {try {statement.close();} catch (SQLException e) {e.printStackTrace();}}if (connection ! null) {try {connection.close();} catch (SQLException e) {e.printStackTrace();}}} }使用方法 省略了设置数据源参数和关闭数据源的操作 public class Main{public static void main(String[] args) {//声明三个对象Connection connection null;PreparedStatement statement null;ResultSet resultSet null;try {// 1. 创建数据源并获取数据库连接connection DBUtil.getConnection();// 2. 构造SQL语句String sql select id, name, sn, email, classId from student order by id ASC;// 使用SQL预处理对象处理SQLstatement connection.prepareStatement(sql);// 3. 执行SQL并获取结果如果是结果集把结果集转成java对象resultSet statement.executeQuery();// 遍历结果集处理查询到数据while (resultSet.next()) {Student student new Student();student.setId(resultSet.getInt(1));student.setName(resultSet.getString(2));student.setSn(resultSet.getLong(3));student.setEmail(resultSet.getString(4));student.setClassId(resultSet.getInt(5));}// 打印结果System.out.println(student);} catch (SQLException e) {e.printStackTrace();} finally {// 4. 释放资源DBUtil.close(resultSet, statement, connection);}} }接收多个查询结果 import java.sql.*; import java.util.ArrayList; import java.util.List;public class Demo02_SelectAll {public static void main(String[] args) {// 查询到的所有学生信息放在List里ListStudent students null;Connection connection null;PreparedStatement statement null;ResultSet resultSet null;try {// 1. 创建数据源并获取数据库连接connection DBUtil.getConnection();// 2. 构造SQL语句String sql select id, name, sn, email, classId from student order by id ASC;// 使用SQL预处理对象处理SQLstatement connection.prepareStatement(sql);// 3. 执行SQL并获取结果如果是结果集把结果集转成java对象resultSet statement.executeQuery();// 遍历结果集处理查询到数据while (resultSet.next()) {// 如果List为空则创建if (students null) {students new ArrayList();}// 解析结果集并封装成Student对象Student student new Student();student.setId(resultSet.getInt(1));student.setName(resultSet.getString(2));student.setSn(resultSet.getLong(3));student.setEmail(resultSet.getString(4));student.setClassId(resultSet.getInt(5));// 加入到集合中students.add(student);}// 打印结果System.out.println(students);} catch (SQLException e) {e.printStackTrace();} finally {// 4. 释放资源DBUtil.close(resultSet, statement, connection);}} }数据更改 如果你想update, 那么把SQL语句即可, 原理是相同的,基本不用改Java代码. 例如改成Update student set age10 where name? 查询时statement.executeQuery(), 设置是statement.executeUpdate() import java.sql.*; import java.util.Scanner;public class Demo03_Delete {public static void main(String[] args) {Connection connection null;PreparedStatement statement null;try {// 1. 获取数据库连接connection DBUtil.getConnection();// 2. 构建SQLString sql delete from student where name ?;// 接收用户的输入System.out.print(请输入要删除的同学姓名:);Scanner scanner new Scanner(System.in);String name scanner.next();// 3. 对SQL进行处理并替换占位符statement connection.prepareStatement(sql);//替换参数: 1代表第一个问号, name代表填入的数据statement.setString(1, name);// 4. 执行SQL并获取结果int row statement.executeUpdate();if (row 0) {System.out.println(删除失败.);} else {System.out.println(删除成功);}} catch (SQLException e) {e.printStackTrace();} finally {// 释放资源DBUtil.close(null, statement, connection);}} }插入数据 import java.sql.*; import java.util.Scanner;public class Demo04_Insert {public static void main(String[] args) {Connection connection null;PreparedStatement statement null;try {// 1. 获取数据库连接connection DBUtil.getConnection();// 2. 构建SQLString sql insert into student values (?, ?, ?, ?, ?);// 接收用户的输入Scanner scanner new Scanner(System.in);System.out.print(请输入要添加的同学Id:);int id scanner.nextInt();// 姓名System.out.print(请输入学生的姓名:);String name scanner.next();// 学号System.out.print(请输入学生的学号-);long sn scanner.nextLong();// 邮箱System.out.print(请输入学生的邮箱-);String email scanner.next();// 班级编号System.out.print(请输入学生的班级编号-);int classId scanner.nextInt();// 3. 对SQL进行处理statement connection.prepareStatement(sql);// 替换占位符statement.setInt(1, id);statement.setString(2, name);statement.setLong(3, sn);statement.setString(4, email);statement.setInt(5, classId);// 4. 执行SQL并获取结果int row statement.executeUpdate();if (row 0) {System.out.println(添加失败.);} else {System.out.println(添加成功);}} catch (SQLException e) {e.printStackTrace();} finally {// 释放资源DBUtil.close(null, statement, connection);}} }
http://www.hkea.cn/news/14561996/

相关文章:

  • 怎样评价一个网站做的好与不好营销公关名词解释
  • 网站制作哪些公司好有什么网站是帮别人做设计的
  • 网页设计案例欣赏邢台做网站优化费用
  • 网站开发专业主修课程桂林网站设计
  • 怎么用模板建站广东网络seo推广公司
  • 怎么依赖网站开发app杭州优化建筑设计
  • 网站建设质量保证金怀化网页
  • 怎样免费建立自己网站国内网页设计
  • 安卓网站开发前景商城网站建设代理商
  • 网站投票功能长沙网络科技有限公司
  • 博物馆网站建设经费请示免费空间送二级域名
  • 特色设计网站推荐织梦txt网站地图制作
  • 宿迁 网站制作网络营销的主要特点及举例
  • 个人网站自助建站中国seo第一人
  • 用php做的网站源代码网站建设教程论坛
  • 太原营销型网站建设职业技术培训
  • 韶关建网站制作英文网站费用
  • 国企公司网站制作wordpress阅读量的统计
  • 图表设计 网站wordpress 源码详解
  • 长沙最好网站建设成都网站建设有限公司
  • 企点财税重庆网站优化排名软件方案
  • 胶州专业网站建设公司网站轮播图能用什么软件做
  • 昆明制作网站公司阿里巴巴外贸圈论坛
  • 化妆品网站建设版块2019年最好的国外vps
  • 福建省住房和城乡建设厅门户网站注册公司每年需要缴纳什么费用
  • 太原做网站推广的公司关键词代发排名首页
  • 无锡网站建设哪家做得比较好电商网站开发有前台吗
  • 锚文本外链网站柳州市安全教育平台
  • 企业网站建设维护方案长春火车站到龙嘉机场高铁时刻表
  • 为什么网站上传都上传不成功电商网站后台功能