JDBC 数据库连接操作——实习第三天

今天开始了比较重量级的学习了,之前都是对于Java基础的学习和回顾。继续上篇的话题,《谁动了我的奶酪》,奉献一句我觉得比较有哲理的话:“学会自嘲了,而当人们学会自嘲,能够嘲笑自己的愚蠢和所做的错事时,他就在开始改变了。他甚至觉得,改变自己的最快方式,就是坦然嘲笑自己的愚笨——这样,你就能对过往云烟轻松释然,迅速行动起来,直面变化。”好了,开始我们的复习~

 

连接数据库所需要的包

除此之外,我们还需要进行导入:

新建一个lib文件夹,然后右击jar包-——>Build path——>选择第一个add to ...

导入成功以后,再开始分析代码:

在数据库创建一个student表:

对应的Student实体类为:

package com.lanou.JDBC;public class Student {private String name;private int age;private String gender;private String className;private int number;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}}

 

 连接数据库,并进行数据库的查询:

package com.lanou.JDBC;
import java.sql.*;
import java.util.ArrayList;public class JDBCTest {public static void main(String[] args) {//加载驱动try {Class.forName("com.mysql.jdbc.Driver");System.out.println("加载成功!");//建立链接try {Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/ZJJ","root","root");//创建一个sql语句对象Statement statement = connection.createStatement();//要执行的sql语句String sql = "select *from student";//执行sqlResultSet set= statement.executeQuery(sql);//创建list保存结果ArrayList<Student> list = new ArrayList<Student>();while (set.next()) {Student s = new Student();//获取数据的两种方式://1.通过数据库的角标,从1开始//2.通过它的名称来获取                    s.setName(set.getString(1));System.out.println(set.getString(1));s.setAge(set.getInt(2));s.setGender(set.getString("gender"));s.setClassName(set.getString("className"));s.setNumber(set.getInt("stuNum"));list.add(s);}    } catch (SQLException e) {// TODO Auto-generated catch block
                e.printStackTrace();}            } catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println("加载驱动失败!");e.printStackTrace();}}
}

我这里只是输出姓名,不过目前已经存在list里面,如果要访问所有的数据,可以对list进行遍历~

 

另:

连接数据库的方式也有这种:

private String className = "com.mysql.jdbc.Driver";private String dburl = "jdbc:mysql://localhost/ZJJ";private String user = "root";private String password = "root";

在SSH框架中,差不多。

 

今天已经开始了整体项目,但是由于没有完成就暂不更新~

 

转载于:https://www.cnblogs.com/zxcjj/p/7061492.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392374.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

webassembly_WebAssembly的设计

webassemblyby Patrick Ferris帕特里克费里斯(Patrick Ferris) WebAssembly的设计 (The Design of WebAssembly) I love the web. It is a modern-day superpower for the dissemination of information and empowerment of the individual. Of course, it has its downsides …

leetcode 509. 斐波那契数(dfs)

斐波那契数&#xff0c;通常用 F(n) 表示&#xff0c;形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始&#xff0c;后面的每一项数字都是前面两项数字的和。也就是&#xff1a; F(0) 0&#xff0c;F(1) 1 F(n) F(n - 1) F(n - 2)&#xff0c;其中 n > 1 给你 n &a…

java基本特性_Java面试总结之Java基础

无论是工作多年的高级开发人员还是刚入职场的新人&#xff0c;在换工作面试的过程中&#xff0c;Java基础是必不可少的面试题之一。能不能顺利通过面试&#xff0c;拿到自己理想的offer&#xff0c;在准备面试的过程中&#xff0c;Java基础也是很关键的。对于工作多年的开发人员…

plotly python_使用Plotly for Python时的基本思路

plotly pythonI recently worked with Plotly for data visualization on predicted outputs coming from a Machine Learning Model.我最近与Plotly合作&#xff0c;对来自机器学习模型的预测输出进行数据可视化。 The documentation I referred to : https://plotly.com/pyt…

转发:毕业前的赠言

1、找一份真正感兴趣的工作。 “一个人如果有两个爱好&#xff0c;并且把其中一个变成自己的工作&#xff0c;那会是一件非常幸福的事情。那么另外一个爱好用来做什么&#xff1f;打发时间啦。所以&#xff0c;第二个兴趣非常重要&#xff0c;在你无聊寂寞的时候越发显得它…

Python模块之hashlib:提供hash算法

算法介绍 Python的hashlib提供了常见的摘要算法&#xff0c;如MD5&#xff0c;SHA1等等。 什么是摘要算法呢&#xff1f;摘要算法又称哈希算法、散列算法。它通过一个函数&#xff0c;把任意长度的数据转换为一个长度固定的数据串&#xff08;通常用16进制的字符串表示&#xf…

css flexbox模型_完整CSS课程-包括flexbox和CSS网格

css flexbox模型Learn CSS in this complete 83-part course for beginners. Cascading Style Sheets (CSS) tell the browser how to display the text and other content that you write in HTML.在这本由83部分组成的完整课程中&#xff0c;为初学者学习CSS。 级联样式表(CS…

leetcode 830. 较大分组的位置

在一个由小写字母构成的字符串 s 中&#xff0c;包含由一些连续的相同字符所构成的分组。 例如&#xff0c;在字符串 s “abbxxxxzyy” 中&#xff0c;就含有 “a”, “bb”, “xxxx”, “z” 和 “yy” 这样的一些分组。 分组可以用区间 [start, end] 表示&#xff0c;其中…

php 匹配图片路径_php正则匹配图片路径原理与方法

下面我来给大家介绍在php正则匹配图片路径原理与实现方法&#xff0c;有需要了解的朋友可进入参考参考。提取src里面的图片地址还不足够&#xff0c;因为不能保证那个地址一定是绝对地址&#xff0c;完全的地址&#xff0c;如果那是相对的呢&#xff1f;如果地址诸如&#xff1…

java项目经验行业_行业研究以及如何炫耀您的项目

java项目经验行业苹果 | GOOGLE | 现货 | 其他 (APPLE | GOOGLE | SPOTIFY | OTHERS) Editor’s note: The Towards Data Science podcast’s “Climbing the Data Science Ladder” series is hosted by Jeremie Harris. Jeremie helps run a data science mentorship startup…

MongoDB教程-使用Node.js从头开始CRUD应用

In this MongoDB Tutorial from NoobCoder, you will learn how to use MongoDB to create a complete Todo CRUD Application. This project uses MongoDB, Node.js, Express.js, jQuery, Bootstrap, and the Fetch API.在NoobCoder的MongoDB教程中&#xff0c;您将学习如何使…

leetcode 399. 除法求值(bfs)

给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件&#xff0c;其中 equations[i] [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。 另有一些以数组 queries 表示的问题&#xff0c;其中 queries[j]…

【0718作业】收集和整理面向对象的六大设计原则

面向对象的六大设计原则 &#xff08;1&#xff09;单一职责原则——SRP &#xff08;2&#xff09;开闭原则——OCP &#xff08;3&#xff09;里式替换原则——LSP &#xff08;4&#xff09;依赖倒置原则——DIP &#xff08;5&#xff09;接口隔离原则——ISP &#xff08;…

数据科学 python_适用于数据科学的Python vs(和)R

数据科学 pythonChoosing the right programming language when taking on a new project is perhaps one of the most daunting decisions programmers often make.在进行新项目时选择正确的编程语言可能是程序员经常做出的最艰巨的决定之一。 Python and R are no doubt amon…

如何进行有效的需求调研

一、什么是需求调研&#xff1f;需求调研对于一个应用软件开发来说&#xff0c;是一个系统开发的开始阶段&#xff0c;它的输出“软件需求分析报告”是设计阶段的输入&#xff0c;需求调研的质量对于一个应用软件来说&#xff0c;是一个极其重要的阶段&#xff0c;它的质量在一…

java中直角三角形第三条边,Java编程,根据输入三角形的三个边边长,程序能判断三角形类型为:等边、等腰、斜角、直角三角形,求代码...

private static Scanner sc;private static int edge[] new int[3];public static void main(String[] args) {System.out.println("请输入三角形的三条边");sc new Scanner(System.in);input();}public static void input() {int index 0;//数组下标while (sc.ha…

react中使用构建缓存_使用React和Netlify从头开始构建电子商务网站

react中使用构建缓存In this step-by-step, 6-hour tutorial from Coding Addict, you will learn to build an e-commerce site from scratch using React and create-react-app.在这个Coding Addict的分步&#xff0c;为时6小时的教程中&#xff0c;您将学习使用React和creat…

Django+Vue前后端分离项目的部署

部署静态文件&#xff1a; 静态文件有两种方式 1&#xff1a;通过django路由访问 2&#xff1a;通过nginx直接访问 方式1&#xff1a; 需要在根目录的URL文件中增加 url(r^$, TemplateView.as_view(template_name"index.html")),作为入口&#xff0c;在setting中更改…

leetcode 547. 省份数量(bfs)

有 n 个城市&#xff0c;其中一些彼此相连&#xff0c;另一些没有相连。如果城市 a 与城市 b 直接相连&#xff0c;且城市 b 与城市 c 直接相连&#xff0c;那么城市 a 与城市 c 间接相连。 省份 是一组直接或间接相连的城市&#xff0c;组内不含其他没有相连的城市。 给你一…

r怎么对两组数据统计检验_数据科学中最常用的统计检验是什么

r怎么对两组数据统计检验Business analytics and data science is a convergence of many fields of expertise. Professionals form multiple domains and educational backgrounds are joining the analytics industry in the pursuit of becoming data scientists.业务分析和…