Database connection Learning--a simple address book

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.