Haven't written blog for a long time, write a simple thing warm warm up, share to everybody.
JDBC believes that everyone is not unfamiliar, as long as it is a Java, the initial contact with the EE time is to learn such a thing, who called the program and the database to deal with it! And JDBC is a database to deal with a very basic knowledge, but also relatively close to the bottom, in the actual work of the use of more in fact still more mature framework, such as Hibernate, Mybatis.
But as these mature framework of the underlying JDBC is also we should be mastered, only to understand the JDBC additions and deletions, so that in the future if interested to study hibernate or MyBatis source code when it is better to understand how these mature framework is how to achieve the deletion and change.
To get back to the chase, take a look at our development environment:
Java language, Eclipse development tools, MySQL database, navicat database visualization tools.
Installation and use of the development environment please check the information yourself (very simple), here is not elaborated in detail.
The first step, create a database, use the NAVICAT Database visualization tool to build a database, create a table in the library, a few fields (remember to give ID field, unique primary key, self-increment sequence), and then give two data is good, to test the function,
The second step, to get through the database (this example wants you to knock on their own, not a lot of time, familiar with how JDBC and database dealings, it is illustrated),
The third step, the transformation of the Dbutil class, convenient for the DAO layer to get the database connection, 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"; UNAME = "root"; one private static final String PWD = "root"; the private static Connection conn = null;14 STATIC16 { try18 { 1. Load driver ("Com.mysql.jdbc.Driver"), + //2. Get a connection to the database conn = drivermanager.getconnection (URL, UNAME, PWD), }24 catch (classnotfoundexception E ) ( e.printstacktrace), }28 catch (SQLException e), { e.printstacktrace ( ) }32}33 public static Connection getconnection () { conn;37
Fourth step, create the entity class (for example, we will use the MVC idea to design This example, there is the idea of MVC design, please learn by yourselves, here not to say) the code is as follows:
1 package Com.czgo.model; 2 3 Import java.io.Serializable; 4 5/** 6 * Entity class: Goddess Class 7 * 8 * @author Alanlee 9 * */11 public class Goddess implements Serializable12 {Priv Ate static final long Serialversionuid = 1l;14 15/**16 * Unique PRIMARY key */18 private Integer id;19/**20 * Name */22 private string name;23/**24 * Mobile number */26 private String mobie;27/**28 * Email */30 private string email;31/**32 * Home Address */34 private string address;35-pu Blic integer getId () PNs {setId return id;39}40 (integer ID) = id;44}45, public String getName (), {name;49}50, public void SetName (String Name) THIS.name = name;54}55, public String Getmobie () mobie;59 }60, public void Setmobie (String mobie) $ this.mobie = mobie;64}65, Public String getemail (), {email;69}70, Setemail (String ema IL) this.email = email;74}75 public String getaddress () address;79 }80 bayi public void setaddress (String address) this.address = address;84}85}
The fifth step, the implementation of the DAO layer (here because it is a small example does not write the DAO interface, the actual work of the large project should be to write the DAO interface, easy to maintain and extend the program), 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 Import Com.czgo.db.DBUtil; Import com.czgo.model.Goddess; 12 13/** 14 * Data-tier processing class * * * @author Alanlee * * * * */public class Goddessdao 20 {21/** 22 * Query All Goddesses * @return * @throws SQLException */public list<goddess> query () 28 {list<goddess> goddesslist = new arraylist<goddess> (); 30 31//Get a database connection Con Nection conn = null; ResultSet rs = null; PreparedStatement ptmt = null; Try Notoginseng {conn = Dbutil.getconnection (); max. StringBuilder sb = new St Ringbuilder (); Sb.append ("Select id,name,mobie,email,address from Goddess"); 42 43//database through the database connection operation, implementationAdditions and deletions to check the PTMT = Conn.preparestatement (sb.tostring ()); Ptmt.executequery rs = (); Goddess Goddess = null; while (Rs.next ()) {goddess = new Goddess (); Ss.setid (Rs.getint ("id")); Goddess.setname (rs.getstring ("name")); Goddess.setmobie (rs.getstring ("Mobie")); Goddess.setemail (rs.getstring ("email")); Goddess.setaddress (rs.getstring ("Address")); Goddesslist.add (goddess); 65} (SQLException e) (e.printstacktrace); } All-in-a-. {() {Rs.close (); (ptmt! = null) 76 {Ptmt.close (); 78 } (SQLException e) Bayi {e.printstack Trace (); * * * * * * * * * * return goddesslist; 87} 88 89/** 90 * Query Single goddess * * * @return * * @throws SQLException 94 * * Lic goddess Querybyid (Integer id): {goddess G = null; 98 Connection conn = null;100 PreparedStatement ptmt = null;101 ResultSet rs = null;102 103 try104 {conn = Dbuti L.getconnection (); 106 107 String sql = "" + "SELECT * from imooc_goddess" + "where id=?"; 108 109 ptmt = conn.preparestatement (sql); 111 Ptmt.setint (1, id); 113 rs = ptmt . ExecuteQuery (); while (Rs.next ()) the 117 G = new Goddess (); 118 G.setid (Rs.getint ("id")); 119 G.setname (Rs.getstring ("name")); 120 G.setmobie (rs.getstring ("Mobie")) 121 G.setemail (rs.getstring ("email")); 122 G.setaddress (rs.getstring ("Address")); 123}124}125 catch (SQLException e) 126 {127 E.printstacktrace ();}129 finally130 {131 try132 {133 if (rs! = null) 134 {135 rs.close (); 136}137 If (ptmt! = null) 138 {139 Ptmt.close (); 140}141}142 catch (SQLException e) 143 {144 e.printstacktrace (); 145}146}147 148 return g;149}150 151/**152 * Add Goddess 153 * 154 * @throws SQLException155 */156 publi c void Addgoddess (Goddess Goddess) 157 {158//Get Database connection 159 Connection conn = null;160 PREPAREDST Atement ptmt = null;161162 try163 {164 conn = dbutil.getconnection (); 165 166 String sql = "INSERT INTO go Ddess (name,mobie,email,address) VALUES (?,?,?,?) "; 167 168 ptmt = conn.preparestatement (SQL) 169 (1, ptmt.setstring ()); 171 Ptmt.setstring (2, Goddess.getmobie ()); 172 ptmt.setstring (3, Goddess.getemail ()); 173 Ptmt.sets Tring (4, goddess.getaddress ()); 174 175 Ptmt.execute (); 176}177 catch (SQLException e) 178 {179 e.printstacktrace ();}181 finally182 {183 try184 {1 if (ptmt! = null) 186 {187 ptmt.close (); 188}189 }190 catch (SQLException e) 191 {192 e.printstacktrace (); 193} 194}195}196 197/**198 * Modify Goddess Data 199 * @throws SQLException2*/202 public void Updategoddess (goddess Goddess) 203 {204 Connection conn = null;205 Prepar Edstatement ptmt = null;206 207 try208 {209 conn = dbutil.getconnection (); 210 211 String sql = "Update goddess set name=?,mobie=?,email=?,address=?" where id=? "; 212 213 ptmt = conn.preparestatement (sql); 214 215 ptmt.setstring (1, Goddess.getname ()); 216 Ptmt.setstring (2, Goddess.getmobie ()); 217 ptmt.setstring (3, Goddess.getemail ()); 218 Ptmt.sets Tring (4, goddess.getaddress ()); 219 Ptmt.execute (); 221}222 catch (SQLException e) 223 {224 e.printstacktrace (); 225}226 finally227 {228 try229 {2 if (ptmt! = null) 231 {232 ptmt.close (); 233}234 }235 catch (SQLException e) 236 {237 E.printstacktrace (); 238}239}240}241 242/**243 * Delete Goddess 244 * 245 * @throws SQLException246 */247 public void deletegoddess (Integer id) 248 {249 Connection conn = n ull;250 PreparedStatement ptmt = null;251 252 try253 {254 conn = dbutil.getconnection ( ); 255-String sql = "Delete from goddess where id=?"; 257 258 ptmt = conn.preparestatement (SQL) 259 260 ptmt.setint (1, id); 261 262 Ptmt.exec Ute (); 263}264 catch (SQLException e) 265 {266 e.printstacktrace (); 267}268 finally269 {try271 {272 if (ptmt! = null) 273 {274 Ptmt.close (); 275}276}277 catch (SQLException e) 278 {279 e.printstacktrace (); 280}281}282 }283}
The sixth step, the control layer of the implementation (control layer is used here to imitate the control layer and interface, directly here to build data, if the interface data is passed through the request to receive parameters can be, control layer code can be changed according to the actual situation to improve, here is just to give you a simple test, time is relatively tight, I hope you understand), 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, directly here to build data, interface data through the request to receive, also is the same * * * @author ALANLEE13 * */15 public class GoddessAction16 {1 7/**18 * New Goddess * * @param goddess21 * @throws Exception22 */23 public void Add (Godde SS Goddess) throws Exception24 {Goddessdao dao = new Goddessdao (); Goddess.setname ("Cang jing Empty"); 27 Goddess.setmobie ("52220000"), Goddess.setemail ("[email protected]"), goddess.setaddress ("Beijing Red Light Dao.addgoddess (goddess), 31}32 33/**34 * Query Single goddess * * @param id37 * @return SQLEXCEPTION39 * @throws */40 public goddess get (Integer id) throws SQLException41 {Goddes SDao dao = new Goddessdao (); Dao.querybyid return (ID); 44}45 46/**47 * Modified Goddess 48 * 49 * @param goddess50 * @throws Exception51 */52 public void edit (goddess Goddess) throws Exception53 {Goddessdao dao = new Goddessdao (); dao.updategoddess (goddess); 56}57 58/**59 * Delete Goddess 6 0 * id62 * @param * @throws SQLException63 */64 public void del (Integer id) throws SQLException Goddessdao dao = new Goddessdao (); dao.deletegoddess (ID); 68}69 70/**71 * Query All women God * * * * * * @return74 * @throws Exception75 */76 public list<goddess> query () throws Exceptio n77 {Goddessdao dao = new Goddessdao (); return dao.query (); 80}81 82/**83 * Test succeeded 8 4 * args86 * @param * @throws SQLException87 */88 public static void Main (string[] args) throws SQLException89 {Goddessdao Goddessdao = new Goddessdao (); list<goddess> goddesslist = g Oddessdao.query (); 93 94 for (goddess Goddess:goddesslist) System.out.println (goddess.getname () + "," + Goddess . Getmobie () + "," + Goddess.getemail ()); 97}98}99}
Finally, let's take a look at the success of the main method's running result:
In this way, a simple java JDBC Connection MySQL database implementation additions and deletions are completed, you can try to do a query based on a high-level query, that is, multi-conditional query to consolidate the use of JDBC. Time is tight, so did not give you a test of the function of adding and removing changes, idle nothing to do the egg ache can all go to test, if found the problem, I hope to be able to correct little Alan, little Alan has time to fix some of the errors in the blog post.
Add:
Little Alan in the comments and some friends pulled some questions, after reading the article, remember to look at the following comments, their own to think about, how to improve my buddies and discuss these issues, technology is always learning, the key is to solve the problem of ideas and methods, so that no matter what difficulties encountered or new technology can be met difficult and on the It is also the way to work in the company.
Last modified: July 23, 2016
Cute bo master: Alanlee
Reproduced in: Http://www.cnblogs.com/AlanLee/p/5656640.html#commentform.
JDBC Connection data and operational data