This article describes the operation of Java connecting to databases and displaying data based on JDBC. Share it for your reference, as follows:
1. Import jdbc package
To connect to the MySQL database of java, you need to use JDBC tools (mysql-connector-java-5.1.39-bin.jar). This is a jar package. Different databases correspond to different jar packages. Here is the MySQL database jar package. Importing is very simple. Right-click on the project with the mouse - Build Path - Configure Build Path - Select Libraries - Add External JARs on the right, and just select the correct jdbc package.
2. Create a database (example: database name studentdb, table name stable)
3. Create new attributes and construct method class (stuInfo.java)
public class stuInfo { private int sno; private String sname; private String sex; private int age; public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public stuInfo(){ } public stuInfo(int sno, String sname, String sex, int age) { this.sno = sno; this.sname = sname; this.sex = sex; this.age = age; }}3. Create a new main class (shoeTest.java)
import info.stuInfo;import java.util.ArrayList;import java.sql.*;public class showTest { public static void main(String[] args) { ArrayList<stuInfo> list = getAllStus(); if(list.size() == 0){ System.out.println("No data yet"); }else{ for(stuInfo s: list){ //Transfer the collection data System.out.println(s.getSno()+"/t"+s.getSname()+"/t"+s.getSex()+"/t"+s.getAge()); } } } //Use the collection method to return the data set public static ArrayList<stuInfo> getAllStus(){ ArrayList<stuInfo> stulist = new ArrayList<stuInfo>(); String url = "com.mysql.jdbc.Driver"; //Load the driver package String connectSql = "jdbc:mysql://127.0.0.1:3306/studentdb"; //Link MySQL database String sqlUser = "root"; //Database account String sqlPasswd = "****"; //Your database password Connection con = null; PreparedStatement psm = null; ResultSet rs = null; try { //Load the driver package Class.forName(url); //Connect MYSQL con = DriverManager.getConnection(connectSql,sqlUser,sqlPasswd); //Execute the MYSQL statement psm = con.prepareStatement("select * from stable"); rs = psm.executeQuery(); System.out.println("number"+"/t"+"name"+"/t"+"gender"+"/t"+"age"); while(rs.next()){ stuInfo s = new stuInfo(); s.setSno(rs.getInt(1)); s.setSname(rs.getString(2)); s.setSex(rs.getString(3)); s.setAge(rs.getInt(4)); stulist.add(s); } //Close the database connection rs.close(); psm.close(); con.close(); } catch (Exception e) { System.out.println("Show all data errors, reason: "+e.getMessage()); } return stulist; }}4. Run the test
For more information about Java related content, please check out the topics of this site: "Summary of Java's skills to operate databases using JDBC", "Summary of Java+MySQL database programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java File and Directory Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.