MySQL安裝與idea的連接實(shí)現(xiàn)
MySQL安裝與idea的連接
--編輯my.ini配置文件內(nèi)容(Mysql 8.0以上不需要,直接安裝即可) [mysql] # 設(shè)置mysql客戶(hù)端默認(rèn)字符集 default-character-set=utf8 [mysqld] #設(shè)置3306端口 port = 3306 # 設(shè)置mysql的安裝目錄 basedir=E:\MySQL5.7.13\mysql-5.7.13-winx64 # 設(shè)置mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)的存放目錄 datadir=E:\MySQL5.7.13\mysql-5.7.13-winx64\data # 允許最大連接數(shù) max_connections=200 # 服務(wù)端使用的字符集默認(rèn)為8比特編碼的latin1字符集 character-set-server=utf8 # 創(chuàng)建新表時(shí)將使用的默認(rèn)存儲(chǔ)引擎 default-storage-engine=INNODB # 安裝好后, 免密碼進(jìn)入mysql skip-grant-tables --用管理員身份運(yùn)行cmd,輸入命令 //安裝mysql mysqld -install //安裝成功后,初始化數(shù)據(jù)文件 mysqld --initialize-insecure --user=mysql //進(jìn)入mysql管理界面 mysql -u root-p //修改密碼 update mysql.user set password=password('新密碼') where user='root'; //mysql8修改密碼 alter user 'root'@'localhost' identified by '密碼'
Mysql與idea進(jìn)行連接
1.導(dǎo)入數(shù)據(jù)庫(kù)驅(qū)動(dòng)
點(diǎn)擊連接進(jìn)行下載:(mysql驅(qū)動(dòng))
https://github.com/epochong/mysql-connector-java-8.0.16.git
下載后在idea目錄下新建lib目錄,將下載好的驅(qū)動(dòng)移動(dòng)到lib目錄下,并右擊點(diǎn)擊添加為庫(kù),再次點(diǎn)擊驅(qū)動(dòng)文件,若能展開(kāi),則驅(qū)動(dòng)安裝成功。
連接過(guò)程若出現(xiàn)驅(qū)動(dòng)問(wèn)題,需要注意查看驅(qū)動(dòng)是否添加為庫(kù),英文版(add as library),查看驅(qū)動(dòng)版本的問(wèn)題(下載驅(qū)動(dòng)需要對(duì)應(yīng)與數(shù)據(jù)庫(kù),例mysql下載mysql驅(qū)動(dòng),sql server下載的是sql server驅(qū)動(dòng),查看是否在同一包下,有時(shí)候不在同一包下會(huì)找不到驅(qū)動(dòng))。
2.連接數(shù)據(jù)庫(kù)(最基本的連接方法)
package jdbc_excise; import java.sql.*; public class Jdbc { public static void main(String[] args) throws SQLException { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&useSSL=false"; //通用模板:jdbc:數(shù)據(jù)庫(kù)名字://地址:端口/實(shí)際使用數(shù)據(jù)庫(kù)名稱(chēng)?附加參數(shù) String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url,username,password); Statement statement = connection.createStatement(); //執(zhí)行sql查詢(xún)語(yǔ)句 String sql = "select * from student"; ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()){ System.out.println("Sno="+resultSet.getObject("Sno")); } resultSet.close(); statement.close(); connection.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
**附狂神教程中安全連接解決辦法 **
jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&useSSL=false
若mysql版本高于驅(qū)動(dòng)版本,則需要將安全連接置為false;置為true會(huì)報(bào)錯(cuò)。
封裝工具類(lèi)連接數(shù)據(jù)庫(kù)
編寫(xiě)配置文件
--新建配置文件:db.properties-- driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&useSSL=false username=root password=123456
封裝工具類(lèi)
package connect_jdbc.utils; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JdbcUtils { private static String driver = null;; private static String url =null; private static String username = null; private static String password = null; static { try{ //通過(guò)反射得到配置文件中的內(nèi)容 InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties=new Properties(); properties.load(in); driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); //加載一次驅(qū)動(dòng) Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //獲取連接 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,username,password); } //釋放連接 public static void release(Connection conn, Statement st, ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
編寫(xiě)測(cè)試類(lèi)執(zhí)行sql語(yǔ)句
//執(zhí)行executeUpdate語(yǔ)句,實(shí)現(xiàn)增刪改 package connect_jdbc.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcTest { public static void main(String[] args) throws SQLException { Connection connection =null; Statement st = null; ResultSet rs =null; try { connection = JdbcUtils.getConnection(); } catch (SQLException throwables) { throwables.printStackTrace(); } st = connection.createStatement(); String sql = "insert into student (sno, sname, ssex, sclass, stel, sgroup, spassword)" + "values (1907040136,'賀子奇','男','1900144','15735116626',3,'123456')"; int i = st.executeUpdate(sql);//返回值為整型,表示有幾行受影響 if(i>0){ System.out.println("插入成功!"); } JdbcUtils.release(connection,st,rs); } }
執(zhí)行select語(yǔ)句
//執(zhí)行executeQuery語(yǔ)句,實(shí)現(xiàn)查找 package connect_jdbc.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcSelect { public static void main(String[] args) { Connection connection = null; Statement st = null; ResultSet res = null; try { connection = JdbcUtils.getConnection(); st = connection.createStatement(); String sqls = "select * from student"; res = st.executeQuery(sqls);//返回值為查找的結(jié)果集 while (res.next())//進(jìn)行結(jié)果集的輸出 { System.out.println(res.getObject("sno")+" "+res.getObject("sname")); } } catch (SQLException throwables) { throwables.printStackTrace(); } JdbcUtils.release(connection,st,res); } }
sql注入的問(wèn)題及解決
問(wèn)題描述:在使用statement函數(shù)執(zhí)行sql操作時(shí),當(dāng)輸入sql語(yǔ)句為:’ ‘or’1=1’或者’ 'or’values>0’時(shí)則會(huì)發(fā)生恒等于從而繞過(guò)查詢(xún)語(yǔ)句,會(huì)發(fā)生將結(jié)果集繞過(guò)密碼查詢(xún)出來(lái),從而形成安全威脅。
解決辦法
將原先的statement函數(shù)改用preparedStatement函數(shù),避免了sql注入,查詢(xún)效率更高。
示例:
package connect_jdbc.utils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JdbcTestSe { public static void main(String[] args) { Connection connection =null; PreparedStatement statement = null; ResultSet res = null; try { connection = JdbcUtils.getConnection(); //與statement的區(qū)別,要使用?占位符代替參數(shù),進(jìn)行一次預(yù)編譯 String sql = "insert into student (sno, sname, ssex, sclass, stel, sgroup, spassword)" + "values (?,?,?,?,?,?,?)"; //手動(dòng)給每一個(gè)參數(shù)(?)賦值 statement=connection.prepareStatement(sql); statement.setString(1,"1907040124"); statement.setString(2,"薛曉軍"); statement.setString(3,"男"); statement.setString(4,"19070144"); statement.setString(5,"15735116626"); statement.setString(6,"3"); statement.setString(7,"123456"); //執(zhí)行 int i = statement.executeUpdate(); if(i>0) { System.out.println("插入成功!"); } } catch (SQLException throwables) { throwables.printStackTrace(); } JdbcUtils.release(connection,statement,res); } }
到此這篇關(guān)于MySQL安裝與idea的連接實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL安裝與idea連接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java8實(shí)現(xiàn)Stream流的合并的方法展示
本文介紹了Java8中Stream流的合并方法,包括concat()、flatMap()和reduce()三種方法。其中,concat()方法可以將兩個(gè)Stream流合并成一個(gè),flatMap()方法可以將一個(gè)Stream流中的元素映射成多個(gè)Stream流并合并成一個(gè),reduce()方法可以將Stream流中的元素逐個(gè)合并成一個(gè)結(jié)果2023-05-05Spring思維導(dǎo)圖助你輕松學(xué)習(xí)Spring
這篇文章主要為大家詳細(xì)介紹了Spring思維導(dǎo)圖,幫助你輕松學(xué)習(xí)Spring的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03spring aop execution表達(dá)式的用法
這篇文章主要介紹了spring aop execution表達(dá)式的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步
這篇文章主要為大家詳細(xì)介紹了基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08springboot項(xiàng)目獲取請(qǐng)求頭當(dāng)中的token的方法
本文主要介紹了springboot項(xiàng)目獲取請(qǐng)求頭當(dāng)中的token的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11Java內(nèi)存模型相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了Java內(nèi)存模型相關(guān)知識(shí)總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10SpringBoot 文件或圖片上傳與下載功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot 文件或圖片上傳與下載功能的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法
這篇文章主要介紹了SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法,文章內(nèi)容介紹詳細(xì)需要的小伙伴可以參考一下2022-04-04