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