当前位置:网站首页>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

​JDBC Introduction to ​



原网站

版权声明
本文为[DanielMaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011445160821.html