当前位置:网站首页>JDBC exercises
JDBC exercises
2022-06-12 00:33:00 【DanielMaster】
Programming is right for dept Table implements the following operations
- Set up the project correctly and finish reading the external property file
- Paging display dept Table data information , Each page is displayed 2 strip , And encapsulate the data into Dept Collection .( The parameters are the number of entries per page and the displayed page number )
- towards dept Insert in table "50",“DEVELOPMENT”,“JAP”( Parameter is dept object )
- Delete dept In the table 50 Information of department No ( Parameter is deptno)
Departmental table
package practice;
/**
*
* @author Yang
* Departmental table
*/
public class Dept {
private int deptno;
private String dname;
private String loc;
public Dept() {
}
public Dept(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
JdbcUtils class
package practice;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/** Reading of external property file
* Connection acquisition and resource release
* Solve hard coding problem
*/
public class JdbcUtils {
// It is written in a static code block so that each time a class is loaded , Get the connection before creating the instance , It only takes one time
static String url = null;
static String user = null;
static String password = null;
static {
try {
// With the help of Properties class , Then create an input stream , Use reflection to get the contents
Properties pros = new Properties();
//getResourceAsStream Is load .class In the document jdbc file
InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
// Load mapping pairs
pros.load(is);
is.close();
// obtain value value
url = pros.getProperty("url");
user = pros.getProperty("user");
password = pros.getProperty("password");
// Register once
Class.forName(pros.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// Get the connection , Before getting a connection , The configuration file has been created
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
throw new RuntimeException(" Failed to get connection ");
}
}
// Release resources
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
jdbc.properties file
- stay src Right click New -> File, stay File name In the input jdbc.properties that will do , And then type in the content
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///bd1906?useSSL=true
user=root
password=root
- 1.
- 2.
- 3.
- 4.
TestDept Test class
package practice;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TestDept {
public static void main(String[] args) {
// Pagination 、 Insert 、 Delete
System.out.println(queryAll(1, 2));
System.out.println(queryAll(2, 2));
// System.out.println(insert(new Dept(50,"DEVELOPMENT","JAP")));
// System.out.println(delete(50));
}
// According to the title requirements, the number of incoming pages and the number displayed on each page
public static List<Dept> queryAll(int page, int num) {
List<Dept> list = new ArrayList<Dept>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Call our own class
conn = JdbcUtils.getConnection();
// Paging query in sql Add... In the sentence limit Limit the number of pages
String sql = "select * from dept limit ?,?";
ps = conn.prepareStatement(sql);
// We want to make sure that the first page is (0,2) From 0( It doesn't contain ) Start , Count back two , That is 1,2, The same is true for the second page
ps.setInt(1, (page - 1) * num);
ps.setInt(2, num);
rs = ps.executeQuery();
// obtain
while (rs.next()) {
int deptno = rs.getInt("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
Dept dept = new Dept(deptno, dname, loc);
list.add(dept);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(conn, ps, rs);
}
return list;
}
public static int insert(Dept dept) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int rows = 0;
try {
conn = JdbcUtils.getConnection();
String sql = "insert into dept values(?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, dept.getDeptno());
ps.setString(2, dept.getDname());
ps.setString(3, dept.getLoc());
rows = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(conn, ps, rs);
}
return rows;
}
public static int delete(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int rows = 0;
try {
conn = JdbcUtils.getConnection();
String sql = "delete from dept where deptno =?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rows = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(conn, ps, rs);
}
return rows;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
Inserted and deleted annotation references
边栏推荐
- Jiaming's day13 of C learning -- structure and structure pointer
- 苹果手机wps如何改字体大小
- [case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC
- JS——防止自动恢复页面位置
- Water for a while
- Detailed explanation of merge sorting
- EFCore中数据表的两种配置方式
- Teach you to play with SSM framework
- Openmmlab:ai CV framework
- NFS quotas:Cannot register service: RPC: Authentication error
猜你喜欢

m-way search trees

Industrial control system ICs

Jeecgboot 3.1.0 release, enterprise low code platform based on code generator

Chisel environment setup (win10 + vscode)

苹果手机wps如何改字体大小

预解析与作用域

Cube technology interpretation | detailed explanation of cube applet Technology

MySQL basic tutorial -- MySQL transaction and storage engine

Motianlun domestic database salon | Xu Li: Alibaba cloud's native lindorm TSDB database drives the upgrading of industrial it & ot hyper integrated digital system

Microservice automation
随机推荐
Redis master-slave replication, sentinel mode and cluster
Water for a while
IP编址概述
Environment construction 2
Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade
愉快无负担的跨进程通信方式
Redis的主从复制、哨兵模式和集群
2022 618 notebook shopping guide
Summary of DOM knowledge points
Explore table component virtualization
The road of global evolution of vivo global mall -- multilingual solution
What are the advantages of Tiktok applet
时间选择器样式错乱 中间文字被遮挡
苹果手机wps文件如何发送到qq邮箱
DevOps落地实践点滴和踩坑记录-(1)
Teach you to play with SSM framework
Mmdetection custom fetch detection result script and image_ demo. Py parsing
Visitors push e-commerce express without tossing about personal payment codes
Do you want to take the postgraduate entrance examination? Will you be able to find a good job after graduate school?
926. Flip String to Monotone Increasing