java連接mysql數(shù)據(jù)庫詳細步驟解析
第一步:下載一個JDBC驅(qū)動包,例如我用的是:mysql-connector-java-5.1.17-bin.jar
第二步:導(dǎo)入下載的JDBC驅(qū)動包,我用的是myeclipse,選中自己要導(dǎo)包的項目,右 擊選中propertise,再選JavaBuild Path, 右邊會出現(xiàn)libreries,點進去,再點Add External JARs 然后再找到你要導(dǎo)入的驅(qū)動包。完了之后再點Order andExport,下面再選中你導(dǎo)入的包。
第三步:加載驅(qū)動程序:Class.forName("com.mysql.jdbc.Driver");
第四步:連接數(shù)據(jù)庫:Connection conn=DriverManager.getConnection ("jdbc:mysql://localhost/數(shù)據(jù)庫名稱","root","123456");
第五步:聲明一個Statement 用來執(zhí)行sql語句: Statement stmt=conn.createStatement();
第六步:聲明一個結(jié)果集接住執(zhí)行sql語句的數(shù)據(jù): ResultSet rs=stmt.executeQuery("select * from 表名");
下面給出完整的代碼:
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("測試通過");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/myschool","root","123456");
System.out.println("conn-------------"+conn);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from admin");
while(rs.next()){
String name=rs.getString("name");
String pwd=rs.getString("pwds");
System.out.println("name------"+name+"--------pwd-"+pwd);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
相關(guān)文章
springboot使用GuavaCache做簡單緩存處理的方法
這篇文章主要介紹了springboot使用GuavaCache做簡單緩存處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01SpringBoot Actuator埋點和監(jiān)控及簡單使用
最近做的項目涉及到埋點監(jiān)控、報表、日志分析的相關(guān)知識,于是搗鼓的一番,下面把涉及的知識點及SpringBoot Actuator埋點和監(jiān)控的簡單用法,給大家分享下,感興趣的朋友一起看看吧2021-11-11Java后臺返回和處理JSon數(shù)據(jù)的方法步驟
這篇文章主要介紹了Java后臺返回和處理JSon數(shù)據(jù)的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09WebSocket+Vue+SpringBoot實現(xiàn)語音通話的使用示例
本文主要介紹了WebSocket+Vue+SpringBoot實現(xiàn)語音通話的使用示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11