Connect MySQL with JDBC and use MySQL

Source: Internet
Author: User

The driver is a JDBC driver.
The URL is the address of the database.
Usrname and password are database user names and passwords, respectively.
The connection class is used to connect to MySQL.

The resultset is used to store the result, which is typically used with the SELECT statement.

Import Java.sql.*;class MySQL {Private StaticFinal String Driver ="Com.mysql.jdbc.Driver";Private StaticFinal String URL ="Jdbc:mysql://localhost:3306/school";Private StaticFinal String Usrname ="Root";Private StaticFinal String Password ="";PrivateConnection con =NULL;PrivatePreparedStatement PS =NULL;PrivateResultSet rs =NULL;//Create student table     Public MySQL() {String sql ="CREATE table if not EXISTS student (name Char),"+"Sno Char (Ten) primary key, age smallint, sex char (6),"+"Sdept char (4))";Try{Class.forName (driver); }Catch(ClassNotFoundException e) {//throws an exception with no driver foundE.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password);//Connect to databasePS = con.preparestatement (SQL);//Precompiled SQL statementsPs.executeupdate ();//Update            //Close the corresponding object, note the order problem, irreversible.             Try{if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e)            {E.printstacktrace (); }        }Catch(SQLException e)        {E.printstacktrace (); }    }//Insert a message     Public void Addinfo(string name, String sno,intAge, String sex, String sdept) {String sql ="INSERT into student (name, Sno, age, Sex, sdept)"+"VALUES ('"+ name +"', '"+ Sno +"',"+ Age +", '"+ Sex +"', '"+ Sdept +"');";Try{Class.forName (driver); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password);            PS = con.preparestatement (SQL); Ps.executeupdate ();Try{if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e)            {E.printstacktrace (); }        }Catch(SQLException e)        {E.printstacktrace (); }    }//Delete a message     Public void Deleteinfo(String name) {String sql ="Delete from student where name = '"+ name +"';";Try{Class.forName (driver); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password);            PS = con.preparestatement (SQL); Ps.executeupdate ();Try{if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e)            {E.printstacktrace (); }        }Catch(SQLException e)        {E.printstacktrace (); }    }//Query a piece of information     Public void SearchInfo(String name) {String sql ="SELECT * from student where name is like '"+ name +"';";Try{Class.forName (driver); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password);            PS = con.preparestatement (SQL); rs = Ps.executequery ();//Accept the results of the query and store them in Rs.             if(Rs.next ()) { Do{System. out. println ("Name:"+ rs.getstring (1) +", Sno:"+ rs.getstring (2) +", Age:"+ rs.getstring (3)                             +", Sex:"+ rs.getstring (4) +", sdept:"+ rs.getstring (5)); } while(Rs.next ()); }Else{//If not presentSystem. out. println ("There is no one named"+ name +"."); }Try{if(rs! =NULL) Rs.close ();if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e)            {E.printstacktrace (); }        }Catch(SQLException e)        {E.printstacktrace (); }    }} Public classTESTJDBC { Public Static void Main(string[] args) {System. out. println ("Creating table student!"); MySQL sq =NewMySQL (); System. out. println ("Testing Add data!"); Sq.addinfo ("Mike.","2013210888", A,"Male","CS"); System. out. println ("Testing Delete data!"); Sq.deleteinfo ("Mike."); System. out. println ("Testing Search data!"); Sq.searchinfo ("Mike."); System. out. println ("success!"); }}

Connect MySQL with JDBC and use MySQL

Related Article

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.