•創建一個以JDBC連接資料庫的程序,包含7個步驟:
1、載入JDBC驅動程序:
在連接資料庫之前,首先要載入想要連接的資料庫的驅動到JVM(Java虛擬機),
這通過java.lang.Class類的靜態方法forName(String className)實現。
例如:
try{
//載入MySql的驅動類
Class.forName("com.mysql.jdbc.Driver") ;
}catch(ClassNotFoundException e){
System.out.println("找不到驅動程序類 ,載入驅動失敗!");
e.printStackTrace() ;
}
成功載入後,會將Driver類的實例注冊到DriverManager類中。
2、提供JDBC連接的URL
•連接URL定義了連接資料庫時的協議、子協議、數據源標識。
•書寫形式:協議:子協議:數據源標識
協議:在JDBC中總是以jdbc開始
子協議:是橋連接的驅動程序或是資料庫管理系統名稱。
數據源標識:標記找到資料庫來源的地址與連接埠。
例如:(MySql的連接URL)
jdbc:mysql:
//localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:表示使用Unicode字元集。如果characterEncoding設置為
gb2312或GBK,本參數必須設置為true 。characterEncoding=gbk:字元編碼方式。
3、創建資料庫的連接
•要連接資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,
該對象就代表一個資料庫的連接。
•使用DriverManager的getConnectin(String url , String username ,
String password )方法傳入指定的欲連接的資料庫的路徑、資料庫的用戶名
Ⅱ jdbc怎麼連接mysql資料庫
首先導入驅動jar包,程序是
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connectionMySql {
private static Connection connection;
static{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/purview";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("載入驅動失敗");
e.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
public static void main(String [] args) {
Connection con=connectionMySql.getConnection();
if(!con.equals("")){
System.out.print("資料庫連接成功,連接id是:"+con);
}
}
}
Ⅲ 怎樣用jdbc連接mysql資料庫
importjava.sql.*;
publicclassMysqlTest{
publicstaticvoidmain(String[]args){
//驅動程序名
Stringdriver="com.mysql.jdbc.Driver";
//URL指向要訪問的資料庫名world
Stringurl="jdbc:mysql://127.0.0.1:3306/world";
//MySQL配置時的用戶名
Stringuser="root";
//MySQL配置時的密碼
Stringpassword="123456";
Stringname;
try{
//載入驅動程序
Class.forName(driver);
//連續資料庫
Connectionconn=DriverManager.getConnection(url,user,password);
if(!conn.isClosed())
System.out.println("!");
//statement用來執行SQL語句
Statementstatement=conn.createStatement();
//要執行的SQL語句
Stringsql="select*fromcity";
//結果集
ResultSetrs=statement.executeQuery(sql);
while(rs.next()){
//選擇Name這列數據
name=rs.getString("Name");
//輸出結果
System.out.println(rs.getString("CountryCode")+" "+name);
}
rs.close();conn.close();}
catch(ClassNotFoundExceptione){
System.out.println("Sorry,can`tfindtheDriver!");
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
}
}
Ⅳ 如何使用jdbc連接mysql資料庫
StringdriverName="com.mysql.jdbc.Driver";//載入JDBC驅動
StringdbURL="jdbc:mysql://localhost:3306/test";//連接伺服器和資料庫test
StringuserName="sa";//默認用戶名
StringuserPwd="sa";//密碼
java.sql.ConnectiondbConn;
try{
Class.forName(driverName).newInstance();
dbConn=java.sql.DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("ConnectionSuccessful!");//如果連接成功控制台輸出ConnectionSuccessful!
}catch(ClassNotFoundExceptione){
System.out.println("沒有找到驅動");
}catch(Exceptione){
//TODO:handleexception
e.printStackTrace();
}
還是直接上代碼吧。
Ⅳ jdbc怎麼連接mysql資料庫
String driverName = "com.mysql.jdbc.Driver"; // 載入JDBC驅動String dbURL = "jdbc:mysql://localhost:3306/test"; // 連接伺服器和資料庫testString userName = "sa"; // 默認用戶名String userPwd = "sa"; // 密碼java.sql.Connection dbConn; try { Class.forName(driverName).newInstance(); dbConn = java.sql.DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果連接成功 控制台輸出Connection Successful!} catch (ClassNotFoundException e) { System.out.println("沒有找到驅動");} catch (Exception e) { // TODO: handle exception e.printStackTrace();}
Ⅵ 如何使用jdbc連接mysql資料庫
用Jdbc連接MySql伺服器還是很方便的。
首先,將jdbc導入工程,或者將jdbc放到ClassPath里,這里我利用Eclipse直接導入jdbc jar文件,不羅嗦了。
然後,制定DriverManager,利用最簡單的方法,Class類的froName直接完成,代碼:
Class.forName("com.mysql.jdbc.Driver").newInstance();
然後,實例化一個鏈接Connection,注意用戶名和密碼,有幾個方法可供選擇,這里我用的是DirverManager類的getConnection(String url, String user, String password)方法。具體使用:DriverManager
例如:Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "root", "1234");
下一步,建立用於執行sql語句的Statement,這個容易,一行代碼搞定:
Statement stat=conn.createStatement();
最後就可以利用stat實例執行sql語句了
Ⅶ 如何實現JDBC連接mysql資料庫
1.導入mysql資料庫的驅動jar包
如我的導的是mysql-connector-java-5.1.40.jar
2.注冊驅動
注冊驅動代碼為:Class.forName("com.mysql.jdbc.Driver")
3.獲取連接
Connection con = DriverManager.getConnection(url,username,password)
其中url為 jdbc:mysql://localhost:3306/xun?useSSL=false,後面那個xun為資料庫的庫名,如果不使用加密的話,?useSSL=false這個其實也可以省略,省略後為jdbc:mysql://localhost:3306/xun,不過最好還是別省略哈。
4.獲取PrepareStatement並發送sql查詢語句
String sql = "select * from user where username = ?"(這個查詢語句為隨便舉的例子哈)
PreparedStatement pst = con.preparedStatement(sql);
pst.setString(1,"zengjiaxun");
ResultSet rs = pst.executeQuery();
注意,執行查詢使用的是executeQuery()方法,此方法返回的是ResultSet,ResultSet為結果集,封裝了查詢結果。插入,刪除,修改用的是executeUpdate()方法。
pst.clearParameters();
執行sql後記得關閉相應的流。。
Ⅷ jdbc怎麼連接mysql資料庫
首先導入驅動jar包,程序是
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connectionMySql {
private static Connection connection;
static{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/purview";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("載入驅動失敗");
e.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
public static void main(String [] args) {
Connection con=connectionMySql.getConnection();
if(!con.equals("")){
System.out.print("資料庫連接成功,連接id是:"+con);
}
}
}
Ⅸ 使用jdbc連接mysql為什麼報錯
當我用JDBC連接MySql資料庫時,編譯報了如下錯誤:
錯誤1:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
這要求我們注冊驅動時,把Class.forName("com.mysql.jdbc.Driver");改成 Class.forName("com.mysql.cj.jdbc.Driver");
當我信息滿滿的修改之後重新編譯時,再次出現了錯誤:
錯誤2:
Fri Feb 22 08:55:38 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
這要求我們在設置url參數時,將useSSL=false,修改後 jdbc:mysql://localhost:3306/ds3?useSSL=false
當我修改後,本以為這下應該沒問題了,沒想到,再一次出現了問題
錯誤3:
Exception in thread "main" java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
這要求我們修改時區,修改成jdbc:mysql://localhost:3306/ds3?useSSL=false&serverTimezone=UTC
終於,不在報錯誤了。
錯誤4:當我們配置xml文件時,要把&轉為其本身的轉義字元
配置properties文件的urlurl=jdbc:mysql:///ds3?useSSL=false&serverTimezone=UTC配置xml文件的url<property name="url">jdbc:mysql://localhost:3306/ds3?useSSL=false&serverTimezone=UTC</property>