java 中的mysql調用

來源:互聯網
上載者:User

標籤: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調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.