MySQL calls in Java

Source: Internet
Author: User

Java and MySQL call code Focus records


Using development tools: Idea

Use platform: Ubuntu 14.10


Java section

JDBC driver name and database urlstatic final string jdbc_driver = "Com.mysql.jdbc.Driver"; static final String db_url = "Jdbc:mysql://lo Calhost:3306/student_info ";


Connect to MySQL

Connection conn=null;try{Class.forName (jdbc_driver);} catch (ClassNotFoundException e) {e.printstacktrace ();} try{conn= drivermanager.getconnection (db_url,dbuser,dbpass);} catch (SQLException e) {e.printstacktrace ();}


Use the Select language and prevent injection

Public boolean verify (String user,string password) {    boolean  result=false;    string sql =  "Select * from info where  user=? and password=? ";     connection con = new sql_main (). Getconn ();     Try {        ps = con.preparestatement (SQL);         ps.setstring (1, user);         ps.setstring (2, password);        rs =  Ps.executequery ();        if  (Rs.next ())  {//Verification Success              result=true;             SYSTEM.OUT.PRINTLN (Result);            system.out.println ("User: "  + user);             system.out.println ("Password: "  + password);         }        else {             system.out.println ("Password error  or null ");         }    } catch   (sqlexception e)  {        // TODO  Auto-generated catch block        e.printstacktrace ();     } finally {        try {             if  (rs != null)           &nBsp;      rs.close ();             if  (ps != null)                  ps.close ();             if  (conn != null)                  conn.close ();        } catch  ( Sqlexception e)  {            //  todo auto-generated catch block             e.printstacktrace ();         }    }     return result;}


MySQL section:

The command to use:

SHOW DATABASES; Display Database

Use MySQL; Using MySQL Tables

SELECT * from MySQL; List all MySQL fields

SELECT id,user from MySQL; List IDs, and user

SELECT * from MySQL where id=1; List id=1 rows

DESC MySQL; List table structure

DELETE from MySQL WHERE id=1; Delete a row of id=1

INSERT into MySQL (id,passwd) VALUES (1,123456); Insert a piece of data

ALTER TABLE ' MySQL ' ADD unique (' id ');


Inserting database standards

CREATE TABLE ' Info` (  `ID` int( One) not NULL auto_increment, 

 `name` Char( -) not NULL DEFAULT '' COMMENT 'name', 

 `URL` varchar(255) not NULL DEFAULT '', 

 `Sort` int( One) not NULL DEFAULT '0' COMMENT 'ranking', 

 `Country` Char(Ten) not NULL DEFAULT '' COMMENT 'National',  PRIMARY KEY (`ID`)

) ENGINE=InnoDB auto_increment=Ten DEFAULT CHARSET=UTF8;



This article from the "Linux Learning Notes" blog, declined to reprint!

MySQL calls in Java

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.