String url="jdbc:mysql://localhost:3306/sample_db?user=rootpassword=your_password";
創新互聯建站服務項目包括房縣網站建設、房縣網站制作、房縣網頁制作以及房縣網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,房縣網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到房縣省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
例如:
import?java.sql.DriverManager;
import?java.sql.ResultSet;
import?java.sql.SQLException;
import?java.sql.Connection;
import?java.sql.Statement;
public?class?MysqlDemo?{
public?static?void?main(String[]?args)?throws?Exception?{
Connection?conn?=?null;
String?sql;
//?MySQL的JDBC?URL編寫方式:jdbc:mysql://主機名稱:連接端口/數據庫的名稱?參數=值
//?避免中文亂碼要指定useUnicode和characterEncoding
//?執行數據庫操作之前要在數據庫管理系統上創建一個數據庫,名字自己定,
//?下面語句之前就要先創建javademo數據庫
String?url?=?"jdbc:mysql://localhost:3306/javademo?"
+?"user=rootpassword=rootuseUnicode=truecharacterEncoding=UTF8";
try?{
//?之所以要使用下面這條語句,是因為要使用MySQL的驅動,所以我們要把它驅動起來,
//?可以通過Class.forName把它加載進去,也可以通過初始化來驅動起來,下面三種形式都可以
Class.forName("com.mysql.jdbc.Driver");//?動態加載mysql驅動
//?or:
//?com.mysql.jdbc.Driver?driver?=?new?com.mysql.jdbc.Driver();
//?or:
//?new?com.mysql.jdbc.Driver();
System.out.println("成功加載MySQL驅動程序");
//?一個Connection代表一個數據庫連接
conn?=?DriverManager.getConnection(url);
//?Statement里面帶有很多方法,比如executeUpdate可以實現插入,更新和刪除等
Statement?stmt?=?conn.createStatement();
sql?=?"create?table?student(NO?char(20),name?varchar(20),primary?key(NO))";
int?result?=?stmt.executeUpdate(sql);//?executeUpdate語句會返回一個受影響的行數,如果返回-1就沒有成功
if?(result?!=?-1)?{
System.out.println("創建數據表成功");
sql?=?"insert?into?student(NO,name)?values('2012001','陶偉基')";
result?=?stmt.executeUpdate(sql);
sql?=?"insert?into?student(NO,name)?values('2012002','周小俊')";
result?=?stmt.executeUpdate(sql);
sql?=?"select?*?from?student";
ResultSet?rs?=?stmt.executeQuery(sql);//?executeQuery會返回結果的集合,否則返回空值
System.out.println("學號\t姓名");
while?(rs.next())?{
System.out
.println(rs.getString(1)?+?"\t"?+?rs.getString(2));//?入如果返回的是int類型可以用getInt()
}
}
}?catch?(SQLException?e)?{
System.out.println("MySQL操作錯誤");
e.printStackTrace();
}?catch?(Exception?e)?{
e.printStackTrace();
}?finally?{
conn.close();
}
}
}
這樣:
jdbc:mysql://hostname[:3306]/dbname
jdbc:mysql://localhost:3306/db_librarySys
Connection?conn?=?DriverManager.getConnection?("jdbc:mysql://localhost:3306/db_librarySys?user=rootpassword=1234");
Connection?conn?=?DriverManager.getConnection?("jdbc:mysql://localhost:3306/db_librarySys",?"root",?"1234");
擴展資料:
注意事項
URL=協議名+子協議名+數據源名。
1、協議名總是“jdbc”。
2、子協議名由JDBC驅動程序的編寫者決定。
3、數據源名也可能包含用戶與口令等信息;這些信息也可單獨提供。
URL:jdbc:oracle:thin:@machine_name:port:dbname
注:machine_name:數據庫所在的機器的名稱;
port:端口號,默認是1521
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl為數據庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
注意:Oracle的URL有兩種寫法:
1、jdbc:oracle:thin:@localhost:1521:databaseName? ?常用操作sql的工具:sqlDeveloper.exe,還可以用其他數據庫,如mysql等
2、jdbc:oracle:oci:@localhost:1521:databaseName? ? 用來操作SQL的工具只能用:PL/SQL Developer;數據庫集群時候常用此連接,比上面那個多點功能,性能好點。
mysql5.0
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/testspringboot?useSSL=false
mysql8.0
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/testspringboot?useSSL=falseserverTimezone=UTC
1、首先登陸mysql,查看mysql的數據情況,select * from test_data1 t
2、新建java類,測試jdbc功能
3、編寫java的jdbc代碼,
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/testdb?characterEncoding=utf8useSSL=false";
String user = "root";
String pwd = "123456";
4、代碼中查詢mysql數據表,并執行查出表中內容;select * from test_data1
新聞名稱:url怎么鏈接mysql 連接mysql url
本文鏈接:http://m.2m8n56k.cn/article8/doggcop.html
成都網站建設公司_創新互聯,為您提供用戶體驗、云服務器、、企業網站制作、外貿網站建設、服務器托管
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯