In order to do the graduation design, learning Java, and then to connect the database, in order to connect the database to learn to do a simple small project, contacts (now only add features), successfully connected to the database
First look at my Web home page:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/96/0C/wKioL1kcQNmBzEr2AADJnAOoR8w473.png "title=" 1.png "Width=" "height=" 254 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:500px;height:254px; "alt=" Wkiol1kcqnmbzer2aadjnaoor8w473.png "/>
More simple, then fill in the contact page
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/96/0C/wKiom1kcQXjDdzkRAACqI79EFI8349.png "title=" 2.png "Width=" "height=" 245 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:500px;height:245px; "alt=" Wkiom1kcqxjddzkraacqi79efi8349.png "/>
My database connection code first thrown, after all, this is my Address book learning Focus,
package s2.jsp.zhangxiao.dao;import java.sql.preparedstatement;import java.sql.connection; Import java.sql.resultset;import java.sql.drivermanager;import java.sql.sqlexception;public class AddressBase { // define the user name of the database Private final string dbname = "Root";// defines the password for the database private final string dbpass = "0";// Define driver information for the database private final string driver = "Com.mysql.jdbc.Driver";// Define the address of the Access database private final string url = "Jdbc:mysql://localhost:3306/addressdb"; public connection getconnection () {Connection con=null;try {class.forname (DRIVER); con=drivermanager.getconnection (Url, dbname, dbpass);} catch (classnotfoundexception e) {system.out.println ("Driver exception"); E.printstacktrace ();} catch (sqlexception e) {system.out.println ("Database Exception"); E.printstacktracE ();} Return con; } public void closeall (Connection con, Preparedstatement past,resultset rs) { try {rs.close ();} catch (sqlexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} try {past.close ();} catch (sqlexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} try {con.close ();} catch (sqlexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} } public int update (string sql,string[]getvalues) {int i=0; connection con=null; preparedstatement past=null; Resultset rs=null;con=getconnection (); try {past=con.preparestatement (SQL);if (getValues!=null) {for (int j = 0; j < getvalues.length; j++) { Past.setstriNg (J+1, getvalues[j]);}} I=past.executeupdate ();} catch (sqlexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} return i; }}
This article is from the "777 Fast" blog, be sure to keep this source http://10324228.blog.51cto.com/10314228/1926791
Database connection Learning--a simple address book