安裝mysql後,java使用jdbc串連mysql資料庫

來源:互聯網
上載者:User

標籤:安裝mysql   work   imp   desc   建立   void   ima   uil   姓名   

1.下載安裝mysql,因為開源免費的,直接到官網下載並按提示安裝:https://www.mysql.com/downloads/

2.安裝完成後查看進程是否有開啟mysql(安裝mysql一般會內建mysql workbench,這是資料庫圖形操作介面,我們使用java是不需要用到的)

用cmd(命令列)去操作mysql,在mysql中增添資料庫study,然後再study資料庫中增加表study

3.下載java的驅動來串連mysql,:https://dev.mysql.com/downloads/connector/j/,下載得到一個jar包,哪個工程要用到就複製到工程目錄下,然後add build加入工程(加入工程後可以在referenced Libraries中找到該jar包)

 

4.java代碼:

package com.cn.edu.szu.ming;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * @ClassName:testdb.java* @author [email protected]@email.szu.edu.cn* @version 建立時間:2017年5月30日 上午11:41:40 * @Description:*/public class testdb {    private final static String URL="jdbc:mysql://localhost:3306/study";       //串連格式jdbc:mysql://(ip地址):(連接埠)/(資料庫名)       private final static String NAME="ming";                //mysql使用者名稱    private final static String PASS="qq147741";            //對應的密碼    static Connection conn;    static Statement stmt;    static PreparedStatement pst;    static ResultSet res;    /**     *      * @Tiltle main     * @param argv     * @return void     */    public static void main(String argv[]){        try {            Class.forName("com.mysql.jdbc.Driver");         //載入驅動,string為驅動名字            conn=DriverManager.getConnection(URL,NAME,PASS);    //串連資料庫            System.out.println(conn);            stmt=conn.createStatement();            printTable();                                    //修改資料,將第一行的密碼改為line1            pst=conn.prepareStatement("update study set name=?where id=?");            pst.setString(1, "stu1");     //update的sql語句中第一個參數即pawd,設定為yes            pst.setInt(2, 1);            //update的sql語句第二個參數設定為            pst.executeUpdate();       //發送操作            printTable();                                                            //刪除資料,刪除表中指定的第一行資料            pst=conn.prepareStatement("delete from study where id=?");            pst.setInt(1,3);     //指定上述sql第一個參數即id的值為3            pst.executeUpdate();            printTable();                                    //增加行            pst=conn.prepareStatement("insert into study(id,name,pawd)values(?,?,?)");            pst.setInt(1,4);                   //設定sql語句中第一個參數(study表中為id)為4            pst.setString(2,"stu3");            pst.setString(3,"123456789");            pst.executeUpdate();            printTable();                                } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }        public static void printTable() throws SQLException{        //查詢表,列印study表        res=stmt.executeQuery("select * from study");       //選擇表中所有資訊        System.out.println("編號"+"\t"+"姓名"+"\t"+"密碼");        while(res.next()){           //列印所有行            System.out.print(res.getInt(1)+"\t");           //列印study表中每一行的第一個參數值(即id)            System.out.print(res.getString(2)+"\t");        //列印study表中每一行的第二個參數值(即name)            System.out.print(res.getString(3)+"\n");        //列印study表中每一行的第三個參數值(即pawd)                    }    }}

 更詳細內容推薦閱讀:

詳細描述:http://www.jianshu.com/p/dc73ee0f2f83

sql文法:http://www.w3school.com.cn/sql/sql_syntax.asp

安裝mysql後,java使用jdbc串連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.