Java jdbc connects to the mysql database for addition, deletion, modification, and query, and jdbcmysql
I haven't written a blog post for a long time. I want to warm up and share some simple things with you.
Jdbc is believed to be familiar to everyone. As long as it is engaged in java, it is necessary to learn such a thing when we first came into contact with j2ee. Who is calling a program to deal with databases! Jdbc is a very basic knowledge for dealing with databases and is close to the underlying layer. In actual work, we usually use more mature frameworks, such as Hibernate and Mybatis.
However, the underlying jdbc of these mature frameworks should also be mastered. Only after learning about the addition, deletion, modification, and query of jdbc, in this way, if you are interested in studying the source code of Hibernate or Mybatis, you can better understand how these mature frameworks implement addition, deletion, modification, and query.
Back to the question, let's take a look at our development environment:
Java language, Eclipse development tool, Mysql database, Navicat database visualization tool.
For more information about how to install and build the development environment, see the document (very simple.
Step 1: Create a database, use Navicat database visualization tool to create a database, and create a table in the database. The table contains several fields (remember to give an id field, a unique primary key, auto-incrementing sequence), and then just give the last two pieces of data to test the function,
Step 2: Get through the database (in this example, we hope that you will not be able to get familiar with jdbc and how to deal with the database, as shown in the figure below ),
Step 3: Modify the DBUtil class to facilitate database connection at the dao layer. The Code is as follows:
1 package com. czgo. db; 2 3 import java. SQL. connection; 4 import java. SQL. driverManager; 5 import java. SQL. SQLException; 6 7 public class DBUtil 8 {9 private static final String URL = "jdbc: mysql: // 127.0.0.1: 3306/imooc "; 10 private static final String UNAME = "root"; 11 private static final String PWD = "root"; 12 13 private static Connection conn = null; 14 15 static16 {17 try18 {19 // 1. load the driver 20 Class. forName ("com. mysql. jdbc. driver "); 21 // 2. obtain the database connection 22 conn = DriverManager. getConnection (URL, UNAME, PWD); 23} 24 catch (ClassNotFoundException e) 25 {26 e. printStackTrace (); 27} 28 catch (SQLException e) 29 {30 e. printStackTrace (); 31} 32} 33 34 public static Connection getConnection () 35 {36 return conn; 37} 38}
Step 4: Create an entity class (for example, if you observe the package allocation, we will use the MVC idea to design this instance. Please learn about the mvc design idea on your own. I will not talk about it here) the Code is as follows:
1 package com. czgo. model; 2 3 import java. io. serializable; 4 5/** 6 * entity class: goddess category 7*8 * @ author AlanLee 9*10 */11 public class Goddess implements Serializable12 {13 private static final long serialVersionUID = 1L; 14 15/** 16 * unique primary key 17 */18 private Integer id; 19/** 20 * name 21 */22 private String name; 23/** 24 * mobile phone number 25 */26 private String mobie; 27/** 28 * email 29 */30 private String email; 31/** 32 * home address 33 */34 private String address; 35 36 public Integer getId () 37 {38 return id; 39} 40 41 public void setId (Integer id) 42 {43 this. id = id; 44} 45 46 public String getName () 47 {48 return name; 49} 50 51 public void setName (String name) 52 {53 this. name = name; 54} 55 56 public String getMobie () 57 {58 return mobie; 59} 60 61 public void setMobie (String mobie) 62 {63 this. mobie = mobie; 64} 65 66 public String getEmail () 67 {68 return email; 69} 70 71 public void setEmail (String email) 72 {73 this. email = email; 74} 75 76 public String getAddress () 77 {78 return address; 79} 80 81 public void setAddress (String address) 82 {83 this. address = address; 84} 85}
Step 5: implement the dao layer (because the dao interface is not written in a small example, the dao interface should be written for large projects in actual work to facilitate program maintenance and expansion ), the Code is as follows:
1 package com. czgo. dao; 2 3 import java. SQL. connection; 4 import java. SQL. preparedStatement; 5 import java. SQL. resultSet; 6 import java. SQL. SQLException; 7 import java. util. arrayList; 8 import java. util. list; 9 10 import com. czgo. db. DBUtil; 11 import com. czgo. model. goddess; 12 13/** 14 * data layer processing class 15*16 * @ author AlanLee 17*18 */19 public class GoddessDao 20 {21/** 22 * query all goddess 23*24 * @ retur N 25 * @ throws SQLException 26 */27 public List <Goddess> query () throws SQLException 28 {29 List <Goddess> goddessList = new ArrayList <Goddess> (); 30 31 // obtain the database Connection 32 Connection conn = DBUtil. getConnection (); 33 34 StringBuilder sb = new StringBuilder (); 35 sb. append ("select id, name, mobie, email, address from goddess"); 36 37 // operate the database through the database connection, and add, delete, modify, and query 38 PreparedStatement ptmt = conn. prepareStatement (Sb. toString (); 39 40 ResultSet rs = ptmt.exe cuteQuery (); 41 42 Goddess goddess = null; 43 44 while (rs. next () 45 {46 goddess = new Goddess (); 47 goddess. setId (rs. getInt ("id"); 48 goddess. setName (rs. getString ("name"); 49 goddess. setMobie (rs. getString ("mobie"); 50 goddess. setEmail (rs. getString ("email"); 51 goddess. setAddress (rs. getString ("address"); 52 53 goddessList. add (goddess); 54} 55 r Eturn goddessList; 56} 57 58/** 59 * query a single Goddess 60*61 * @ return 62 * @ throws SQLException 63 */64 public Goddess queryById (Integer id) throws SQLException 65 {66 Goddess g = null; 67 68 Connection conn = DBUtil. getConnection (); 69 70 String SQL = "" + "select * from imooc_goddess" + "where id =? "; 71 72 PreparedStatement ptmt = conn. prepareStatement (SQL); 73 74 ptmt. setInt (1, id); 75 76 ResultSet rs = ptmt.exe cuteQuery (); 77 78 while (rs. next () 79 {80g = new Goddess (); 81g. setId (rs. getInt ("id"); 82g. setName (rs. getString ("name"); 83g. setMobie (rs. getString ("mobie"); 84g. setEmail (rs. getString ("email"); 85g. setAddress (rs. getString ("address"); 86} 87 88 return g; 89} 90 91 /** 92 * Add Goddess 93*94 * @ throws SQLException 95 */96 public void addGoddess (goddess Goddess) throws SQLException 97 {98 // get database Connection 99 Connection conn = DBUtil. getConnection (); 100 101 String SQL = "insert into goddess (name, mobie, email, address) values (?,?,?,?) "; 102 103 PreparedStatement ptmt = conn. prepareStatement (SQL); 104 105 ptmt. setString (1, goddess. getName (); 106 ptmt. setString (2, goddess. getMobie (); 107 ptmt. setString (3, goddess. getEmail (); 108 ptmt. setString (4, goddess. getAddress (); 109 110 ptmt.exe cute (); 111} 112 113/** 114 * modify Goddess profile 115*116 * @ throws SQLException117 */118 public void updateGoddess (goddess Goddess) throws SQLException119 {120 Co Nnection conn = DBUtil. getConnection (); 121 122 String SQL = "update goddess set name = ?, Mobie = ?, Email = ?, Address =? Where id =? "; 123 124 PreparedStatement ptmt = conn. prepareStatement (SQL); 125 126 ptmt. setString (1, goddess. getName (); 127 ptmt. setString (2, goddess. getMobie (); 128 ptmt. setString (3, goddess. getEmail (); 129 ptmt. setString (4, goddess. getAddress (); 130 131 ptmt.exe cute (); 132} 133 134/** 135 * Delete goddess 136*137 * @ throws SQLException138 */139 public void deleteGoddess (Integer id) throws SQLException140 {141 Connectio N conn = DBUtil. getConnection (); 142 143 String SQL = "delete from goddess where id =? "; 144 145 PreparedStatement ptmt = conn. prepareStatement (SQL); 146 147 ptmt. setInt (1, id); 148 149 ptmt.exe cute (); 150} 151}
Step 6: implement the control layer (where the control layer is used to simulate the control layer and interface, and directly construct data here. If the interface data is used, pass the receiving parameters through the request, the code at the control layer can be changed and improved according to the actual situation. Here we will just give you a simple test. The time is tight and I hope you can understand it). The Code is as follows:
1 package com. czgo. action; 2 3 import java. SQL. SQLException; 4 import java. util. list; 5 6 import com. czgo. dao. goddessDao; 7 import com. czgo. model. goddess; 8 9/** 10 * control layer, where data is built directly, and the interface data is transmitted and received through request transmission, similarly, 11*12 * @ author AlanLee13 * 14 */15 public class GoddessAction16 {17/** 18 * Add goddess 19*20 * @ param goddess21 * @ throws Exception22 */23 public void add (Goddess goddess) throws Exception24 {25 GoddessDao dao = new GoddessDao (); 26 goddess. setName ("Aoi"); 27 goddess. setMobie ("52220000"); 28 goddess. setEmail ("52220000@qq.com"); 29 goddess. setAddress ("Beijing red light district"); 30 dao. addGoddess (goddess ); 31} 32 33/** 34 * query a single Goddess 35*36 * @ param id37 * @ return38 * @ throws SQLException39 */40 public Goddess get (Integer id) throws SQLException41 {42 GoddessDao dao = new GoddessDao (); 43 return dao. queryById (id); 44} 45 46/** 47 * Goddess modification 48*49 * @ param goddess50 * @ throws Exception51 */52 public void edit (goddess Goddess) throws Exception53 {54 GoddessDao dao = new GoddessDao (); 55 dao. updateGoddess (goddess); 56} 57 58/** 59 * Delete goddess 60*61 * @ param id62 * @ throws SQLException63 */64 public void del (Integer id) throws SQLException65 {66 GoddessDao dao = new GoddessDao (); 67 dao. deleteGoddess (id); 68} 69 70/** 71 * query all Goddess 72*73 * @ return74 * @ throws Exception75 */76 public List <Goddess> query () throws Exception77 {78 GoddessDao dao = new GoddessDao (); 79 return dao. query (); 80} 81 82/** 83 * test successful 84*85 * @ param args86 * @ throws SQLException87 */88 public static void main (String [] args) throws SQLException89 {90 GoddessDao goddessDao = new GoddessDao (); 91 92 List <Goddess> goddessList = goddessDao. query (); 93 94 for (Goddess goddess: goddessList) 95 {96 System. out. println (goddess. getName () + "," + goddess. getMobie () + "," + goddess. getEmail (); 97} 98} 99}
Finally, let's see if the running result of the main method is successful:
In this way, a simple java jdbc connection to the mysql database is completed by adding, deleting, modifying, and querying. You can try to make an advanced query based on the query, that is, multi-condition queries are used to consolidate the use of jdbc.
At, I haven't had dinner yet, so it's a short time. So I didn't test the addition, deletion, modification, and query function for everyone. I can test it if I have nothing to worry about. If I find any problem, I hope I can correct some mistakes in my blog when Alan is free.
Finally, I wish you all a wonderful dream tonight. Alan went to dinner and will be there again next time!