1.了解jdbc概念
JDBC(Java DataBase Connectivity,java數(shù)據(jù)庫連接)是一種用于執(zhí)行SQL語句的Java API,可以為多種 關系數(shù)據(jù)庫提供統(tǒng)一訪問,它由一組用Java語言編寫的類和接口組成。JDBC提供了一種基準,據(jù)此可以構建 更高級的工具和接口,使數(shù)據(jù)庫開發(fā)人員能夠編寫數(shù)據(jù)庫應用程序,同時,JDBC也是個商標名。
2.java數(shù)據(jù)庫連接
Java數(shù)據(jù)庫連接,(Java Database Connectivity,簡稱JDBC)是Java語言中用來規(guī)范客戶端程序如何 來訪問數(shù)據(jù)庫的[應用程序接口](,提供了諸如查詢和更新數(shù)據(jù)庫中數(shù)據(jù)的方法。JDBC也是Sun Microsystems的商標。我們通常說的JDBC是面向關系型數(shù)據(jù)庫的。
3. JDBC API訪問與執(zhí)行流程
JDBC API 允許用戶訪問任何形式的表格數(shù)據(jù),尤其是存儲在關系數(shù)據(jù)庫中的數(shù)據(jù)。
執(zhí)行流程: 連接數(shù)據(jù)源,如:數(shù)據(jù)庫。
為數(shù)據(jù)庫傳遞查詢和更新指令。
處理數(shù)據(jù)庫響應并返回的結果。?
文章來源:http://www.zghlxwxcb.cn/news/detail-683013.html
pom.xml 中配置
文章來源地址http://www.zghlxwxcb.cn/news/detail-683013.html
?常用操作步驟
1. 加載驅動
//mysql 舊版本驅動名稱,連接mysql 5.1 5.5 5.6
class.forName("com.mysql.jdbc.Driver")
//新版mysql jdbc驅動 兼容舊的mysql數(shù)據(jù)庫 在某此情況可以省略
class.forName("com.mysql.cj.jdbc.Driver")
2.建立數(shù)據(jù)庫連接
//localhost主機 3306端口 null數(shù)據(jù)庫 root賬號 密碼是默認的,所以沒有寫
//Connection conn = DriverManager.getConnection("jdbc:mysql:?
user=root");
//url user password
//Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbshop?
useUnicode=true&characterEncoding=utf8","root","");
String url = "jdbc:mysql://localhost:3306/dbshop";
//url = "jdbc:mysql://localhost:3305/mysql";
String user = "root";
String password = "";
Connection conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
System.out.println(conn.getMetaData().getDatabaseProductVersion());
3. 執(zhí)行相關的語句
4. 關閉連接
public class MyDemo {
public static void main(String[] args) throws
ClassNotFoundException, SQLException {
//1. 加載jdbc for mysql 驅動類
// com.mysql.jdbc.Driver 舊的mysql 5.6 5.5 5.1 5.0
// com.mysql.cj.jdbc.Driver 新的mysql5.7 8.0
Class.forName("com.mysql.cj.jdbc.Driver");
//2. 建立數(shù)據(jù)庫連接 localhost:3306/hbcf user root password nieps
//Connection conn =
DriverManager.getConnection("jdbc:mysql:/hbcf?
user=root&password=nieps");
//jdbc:mysql://127.0.0.1:3306/hbcf?
useUnicode=true&characterEncoding=utf8
//Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3308/wxdb?user=root");
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3308/wxdb? user=root&password=&useUnicode=true&characterEncoding=utf8");
//3. 執(zhí)行相關的sql語句 select * from cf_user;
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from cf_user");
while(rs.next()){
System.out.println(rs.getString("name"));
}
rs.close();
st.close();
//4. 關閉數(shù)據(jù)庫連接
conn.close();
}
}
public class MyTest {
public static void main(String[] args) throws
ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection c =
DriverManager.getConnection("jdbc:mysql://localhost:3308/wxdb?
user=root");
Statement st = c.createStatement();
ResultSet rss = c.createStatement().executeQuery("selectversion()");
rss.next();
System.out.println(rss.getString(1));
System.out.println("--------------------------");
ResultSet rs = st.executeQuery("select * from cf_user");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
rs = st.executeQuery("show tables");
while(rs.next()){
System.out.println(rs.getString(1));
}
}
}
public class MyDemo {
@Test @DisplayName("連接ubuntu mysql5.6.51")
public void m5() {
//01 docker pull mysql:5.6.51
//02 docker run -itd -p 49153:3306 --name m5 -e MYSQL_ROOT_PASSWORD=root-e TZ=Asia/Shanghai mysql:5.6.51
//03 docker run -it --rm mysql:5.6.51 mysql -h192.168.21.68 -uroot -p -
P49153
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.21.68:49153?serverTimezone=PRC";
String user = "root";
String password = "root";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
//String version = conn.getMetaData().getDatabaseProductVersion();
//System.out.println(version);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select now()");
if(rs.next()){
System.out.println(rs.getDate(1)); //java LocalDate mysql date
System.out.println(rs.getTime(1)); //java LocalTime mysql time
System.out.println(rs.getTimestamp(1)); //java LocalDateTime
mysql datetime
}
}catch(Exception e){
e.printStackTrace();
}
}
}
到了這里,關于idea上利用JDBC連接MySQL數(shù)據(jù)庫(8.1.0版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!