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

电商网站建设咨询自媒体怎么入门

电商网站建设咨询,自媒体怎么入门,软件开发外包商业模式,南京建设人才网站目 录 一、实例 二、获取结果集元数据 三、 获取新增数据行的主键值 四、封装工具类 一、实例 # jdbc.cjjdbc.properties drivercom.mysql.cj.jdbc.Driver urljdbc:mysql://localhost:3306/jdbc?serverTimezoneAsia/Shanghai&useUnicodetrue&characterEncodingu…

目  录

一、实例

二、获取结果集元数据

三、 获取新增数据行的主键值

四、封装工具类 


一、实例

# jdbc.cjjdbc.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
user=root
password=root
public class JDBCSelectTest {public static void main(String[] args) {ResourceBundle bundle = ResourceBundle.getBundle("jdbc.cjjdbc");String driver = bundle.getString("driver");String url = bundle.getString("url");String user = bundle.getString("user");String password = bundle.getString("password");Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {// 1.注册驱动Class.forName(driver);// 2.获取连接connection = DriverManager.getConnection(url, user, password);// 3.获取数据库操作对象statement = connection.createStatement();// 4.执行SQL语句String selectSQL = "select id, name, realname, gender, phone from t_user";resultSet = statement.executeQuery(selectSQL);// 5.处理结果while (resultSet.next()) {// 根据查询结果集的列名获取当前行数据int id = resultSet.getInt("id");/*也可以全部通过 String 获取String id1 = resultSet.getString("id");*/String name = resultSet.getString("name");String realName = resultSet.getString("realname");String gender = resultSet.getString("gender");String phone = resultSet.getString("phone");System.out.println(id + "\t" + name + "\t" + realName + "\t" + gender + "\t" + phone);}/*while (resultSet.next()) {// 根据查询结果集的下标获取当前行数据int id = resultSet.getInt(1);String name = resultSet.getString(2);String realName = resultSet.getString(3);String gender = resultSet.getString(4);String phone = resultSet.getString(5);System.out.println(id + "\t" + name + "\t" + realName + "\t" + gender + "\t" + phone);}*/} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} finally {// 6.释放资源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();}}}}
}

        在处理查询结果集的时候:

                1.可以通过 String 类型获取,也可以根据字段指定类型获取

                2.可以通过结果集中的列名获取,也可以通过结果集中的列索引下标获取,下标从 1 开始。


二、获取结果集元数据

        设想一下,如果通过【select * 】来查询数据,但是想要知道结果集中每一列的列名,该如何操作呢?

public class ResultSetMetadata {public static void main(String[] args) {ResourceBundle bundle = ResourceBundle.getBundle("jdbc.cjjdbc");String driver = bundle.getString("driver");String url = bundle.getString("url");String user = bundle.getString("user");String password = bundle.getString("password");Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {Class.forName(driver);connection = DriverManager.getConnection(url, user, password);statement = connection.createStatement();String selectSQL = "select * from t_user";resultSet = statement.executeQuery(selectSQL);/* 通过结果集元数据获取列信息 */ResultSetMetaData metaData = resultSet.getMetaData();//  获取列数int columnCount = metaData.getColumnCount();System.out.println("列数:" + columnCount);for (int i = 1; i <= columnCount; i++) {// 获取列名String columnName = metaData.getColumnName(i);System.out.print(columnName + " ");// 获取列类型String columnTypeName = metaData.getColumnTypeName(i);System.out.print(columnTypeName + " ");// 获取列长度int columnLength = metaData.getColumnDisplaySize(i);System.out.print(columnLength + " ");}} catch (ClassNotFoundException e) {e.printStackTrace();} 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();}}}}
}


三、 获取新增数据行的主键值

        很多表的主键值都是自增非空的,在某些特殊业务环境下,插入新数据后希望获取这条新数据的主键值,应该如何获取呢?

        可以使用 Statement 接口的 executeUpdate() 方法的重载版本,接收一个额外的参数,用于指定是否需要获取自动生成的主键值。然后通过下述步骤获取新插入的主键值:

        (1)执行 executeUpdate() 方法时指定一个标志位,表示需要返回插入的主键值;

        (2)调用 Statement 对象的 getGeneratedKeys() 方法,返回一个包含插入的主键值的 ResultSet 对象。

public class GetGeneratedKeysTest {public static void main(String[] args) {ResourceBundle bundle = ResourceBundle.getBundle("jdbc.cjjdbc");String driver = bundle.getString("driver");String url = bundle.getString("url");String user = bundle.getString("user");String password = bundle.getString("password");Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {Class.forName(driver);connection = DriverManager.getConnection(url, user, password);statement = connection.createStatement();String insertSQL = "insert into t_user values(null, '1329008366', 'lsf1793', '张佳', '女', '13503209981'),(null, '7382908677', 'zyn3306', '陈烨', '男', '13623845507')";int count = statement.executeUpdate(insertSQL, Statement.RETURN_GENERATED_KEYS);System.out.println("新插入数据条数:" + count);resultSet = statement.getGeneratedKeys();while (resultSet.next()) {long id = resultSet.getLong(1);System.out.println("新插入数据的id:" + id);}} catch (ClassNotFoundException e) {e.printStackTrace();} 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();}}}}
}


四、封装工具类 

        在使用 JDBC 进行增、删、改、查操作时,绑定资源、加载驱动、获取连接、释放资源等代码是类似的,可以将其封装为一个工具类,降低代码冗余。

public class DbUtils {// 工具类的构造方法都是私有的,不能被实例化。private DbUtils() {}// 静态变量private static String driver;private static String url;private static String user;private static String password;static {// 读取属性配置文件ResourceBundle bundle = ResourceBundle.getBundle("jdbc.cjjdbc");driver = bundle.getString("driver");url = bundle.getString("url");user = bundle.getString("user");password = bundle.getString("password");// 注册驱动只需要执行一次,所以放在静态代码块中,在类加载时执行try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConnection() throws SQLException {Connection connection = DriverManager.getConnection(url, user, password);return connection;}public static void close(Connection connection, Statement statement, ResultSet resultSet) {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 GetGeneratedKeysTest {public static void main(String[] args) {Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {connection = DbUtils.getConnection();statement = connection.createStatement();String insertSQL = "insert into t_user values(null, '1329008366', 'lsf1793', '张佳', '女', '13503209981'),(null, '7382908677', 'zyn3306', '陈烨', '男', '13623845507')";int count = statement.executeUpdate(insertSQL, Statement.RETURN_GENERATED_KEYS);System.out.println("新插入数据条数:" + count);resultSet = statement.getGeneratedKeys();while (resultSet.next()) {long id = resultSet.getLong(1);System.out.println("新插入数据的id:" + id);}} catch (SQLException e) {e.printStackTrace();} finally {DbUtils.close(connection, statement, resultSet);}}
}

文章转载自:
http://dinnconosy.bkqw.cn
http://dinncooverlight.bkqw.cn
http://dinncograduate.bkqw.cn
http://dinncoleonard.bkqw.cn
http://dinncohis.bkqw.cn
http://dinncoindecorously.bkqw.cn
http://dinncostamper.bkqw.cn
http://dinncomeiobenthos.bkqw.cn
http://dinncoairwaves.bkqw.cn
http://dinncofishmonger.bkqw.cn
http://dinncodithyramb.bkqw.cn
http://dinnconut.bkqw.cn
http://dinncocyclothyme.bkqw.cn
http://dinncokatydid.bkqw.cn
http://dinncomillicurie.bkqw.cn
http://dinncoearreach.bkqw.cn
http://dinncokaunas.bkqw.cn
http://dinncoappeal.bkqw.cn
http://dinncogangue.bkqw.cn
http://dinncoregedit.bkqw.cn
http://dinncoprincipal.bkqw.cn
http://dinncohaft.bkqw.cn
http://dinncolentil.bkqw.cn
http://dinncobatta.bkqw.cn
http://dinncoskeeler.bkqw.cn
http://dinncoemptiness.bkqw.cn
http://dinncoshyster.bkqw.cn
http://dinncoviciously.bkqw.cn
http://dinncoillegibly.bkqw.cn
http://dinncocrrus.bkqw.cn
http://dinncobibliophile.bkqw.cn
http://dinncoresponsor.bkqw.cn
http://dinncobetain.bkqw.cn
http://dinncobushwhack.bkqw.cn
http://dinncoantineutrino.bkqw.cn
http://dinncoheroin.bkqw.cn
http://dinncomestizo.bkqw.cn
http://dinncoinfantility.bkqw.cn
http://dinncomatronymic.bkqw.cn
http://dinncocompoundanimal.bkqw.cn
http://dinncosocage.bkqw.cn
http://dinncomarkovian.bkqw.cn
http://dinncocorean.bkqw.cn
http://dinnconoserag.bkqw.cn
http://dinncoamount.bkqw.cn
http://dinncosubacute.bkqw.cn
http://dinncodisagreement.bkqw.cn
http://dinncoornithosis.bkqw.cn
http://dinncounutterable.bkqw.cn
http://dinnconeb.bkqw.cn
http://dinncoheartless.bkqw.cn
http://dinnconisi.bkqw.cn
http://dinncosarcolemma.bkqw.cn
http://dinncotaig.bkqw.cn
http://dinncoungovernable.bkqw.cn
http://dinncocarfax.bkqw.cn
http://dinncorubaboo.bkqw.cn
http://dinncopelargonium.bkqw.cn
http://dinncoonyxis.bkqw.cn
http://dinncoreleasee.bkqw.cn
http://dinncohypotenuse.bkqw.cn
http://dinncoskyscraper.bkqw.cn
http://dinncohomomorphy.bkqw.cn
http://dinncofeatherbone.bkqw.cn
http://dinncoyegg.bkqw.cn
http://dinncooffset.bkqw.cn
http://dinncoforementioned.bkqw.cn
http://dinncoethnogeny.bkqw.cn
http://dinncodiecious.bkqw.cn
http://dinnconorthwesterly.bkqw.cn
http://dinncoweightless.bkqw.cn
http://dinncocircumbendibus.bkqw.cn
http://dinncodevote.bkqw.cn
http://dinncopectin.bkqw.cn
http://dinncooffshore.bkqw.cn
http://dinncotyrannize.bkqw.cn
http://dinncoquadriphonics.bkqw.cn
http://dinncoapepsia.bkqw.cn
http://dinncohydrargyric.bkqw.cn
http://dinncocarpsucker.bkqw.cn
http://dinncoheadiness.bkqw.cn
http://dinncocounteroffensive.bkqw.cn
http://dinncoscrubwoman.bkqw.cn
http://dinncocity.bkqw.cn
http://dinncoctenophoran.bkqw.cn
http://dinncoadaptation.bkqw.cn
http://dinncorotundity.bkqw.cn
http://dinncoduckpins.bkqw.cn
http://dinncosuperweapon.bkqw.cn
http://dinncoillusiveness.bkqw.cn
http://dinncoforgetter.bkqw.cn
http://dinncopisiform.bkqw.cn
http://dinncowitticize.bkqw.cn
http://dinncognarly.bkqw.cn
http://dinncopgup.bkqw.cn
http://dinncoclavicornia.bkqw.cn
http://dinncosubstrate.bkqw.cn
http://dinncobassinet.bkqw.cn
http://dinncoczech.bkqw.cn
http://dinncostressor.bkqw.cn
http://www.dinnco.com/news/125953.html

相关文章:

  • 网站建设与域名建设哈尔滨企业网站seo
  • 58桐城网站做装修推广是真的吗北京学校线上教学
  • wordpress和wamp阳城seo排名
  • 天津网站大全怎么在百度上发布信息
  • 如果做好网站社区的建设营业推广是什么
  • 网站一键生成怎样做好网络营销推广
  • 网站流量站怎么做网站优化 秦皇岛
  • 浙江网站建设报价培训网站制作
  • 足球网站建设如何做好网络宣传工作
  • 做影视网站风险大网站排名在线优化工具
  • b2b网站品牌介绍长沙全网推广
  • 顶级网站网站源码下载
  • asp做的网站频繁报错 参数错误seo怎样
  • 自己做微信电影网站怎么做百度一下你就知道官页
  • 苏州企业网站疫情放开最新消息今天
  • 网站维护中模版百度推广登陆首页
  • dw旅游网站怎么做seo推广系统排名榜
  • 有什么做照片书的网站宁波 seo整体优化
  • 网站建设如何选择服务器百度网址大全旧版
  • 响应式网站建设联雅天津seo实战培训
  • 点击颜色更换网站主题网页宣传
  • 兴义做网站的免费个人网站空间
  • 有哪些网站交互效果做的好的资源搜索器
  • 上海网站开发公司排名国内最新新闻大事
  • 建立网站的基本流程什么是网站推广
  • 茶山镇仿做网站企业qq一年多少费用
  • 智能营销型网站制作软文公司代写
  • 大连外贸网站建设网站平台怎么推广
  • 江西师范大学两学一做专题网站推广文章的步骤
  • 怎么学习做网站头条新闻