標籤:java和mysql學習總結
java和mysql 調用代碼重點記錄
使用開發工具:idea
使用平台:ubuntu 14.10
java部分
// JDBC 驅動名及資料庫 URLstatic final String JDBC_DRIVER = "com.mysql.jdbc.Driver";static final String DB_URL = "jdbc:mysql://localhost:3306/student_info";
// 串連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();}
//使用select語言,並防注入
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()) {//驗證成功 result=true; System.out.println(result); System.out.println("使用者: " + user); System.out.println("密碼: " + password); } else { System.out.println("password error or null"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (rs != null) 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部分:
使用到的命令:
SHOW DATABASES; //顯示資料庫
USE mysql; //使用mysql表
SELECT * FROM mysql; //列出mysql所有欄位
SELECT id,user FROM mysql; //列出id,和user
SELECT * FROM mysql where id=1; //列出id=1一行
DESC mysql; //列出表結構
DELETE FROM mysql WHERE id=1; //刪除id=1的一行
INSERT INTO mysql (id,passwd) VALUES (1,123456); //插入一條資料
ALTER TABLE `mysql` ADD unique(`id`);
//插入資料庫標準
CREATE TABLE `info` ( `id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL DEFAULT ‘‘ COMMENT ‘名稱‘,
`url` varchar(255) NOT NULL DEFAULT ‘‘,
`sort` int(11) NOT NULL DEFAULT ‘0‘ COMMENT ‘排名‘,
`country` char(10) NOT NULL DEFAULT ‘‘ COMMENT ‘國家‘, PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
本文出自 “linux學習筆記” 部落格,謝絕轉載!
java 中的mysql調用