本文目錄一覽:
- 1、如何載入MySql資料庫驅動
- 2、什麼資料庫驅動 屬性可以連接MySQL資料庫
- 3、如何安裝mysql的odbc連接器
- 4、求java連接MySQL資料庫代碼以及驅動
- 5、mysql連接資料庫找不到合適的驅動
- 6、com.mysql.jdbc.Driver
如何載入MySql資料庫驅動
寫個方法package util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class DBUtil { static String jdbcURL = “jdbc:mysql://localhost:3306/book”;
static String jdbcDriver = “com.mysql.jdbc.Driver”;
static String userName = “root”;
static String password = “root”;
/**
* 獲取資料庫連接對象
* @return 資料庫連接對象
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName(jdbcDriver);
return DriverManager.getConnection(jdbcURL, userName, password);
}
什麼資料庫驅動 屬性可以連接MySQL資料庫
mysql操作類中的資料庫連接對象需要用一個靜態屬性來表示嗎
是這樣的,
訪問資料庫的方式寫成靜態,並不是所有的都是靜態。
即,把資料庫連接寫成靜態。
而查詢、刪除、修改不能寫成靜態。
這樣後,所有的用戶,連接是同一個。
如何安裝mysql的odbc連接器
工具/原料
事先配置相應的環境
mysql(mysql安裝程序)
mysql-connector-odbc-3.51.20-win32.exe(mysql數據源dobc安裝程序)
步驟/方法
1、在網上下載一個是MYSQL資料庫的ODBC驅動程序:mysql-connector-odbc-3.51.20-win32.exe(已上傳),以默認選項安裝該文件。
打開數據源:開始-設置-控制面板-找到數據源
打開數據源(ODBC),在用戶DSN選項卡中點擊「添加」按鈕,彈出「創建新數據源」窗口。
選中「MYSQL
ODBC
5.1
Driver」,點擊「完成」按鈕。
彈出新窗口,在login選項卡中填寫數據源信息。
Data
Source
Name
數據原名稱;
Discription
描述(選填);
Server
數據源計算機的IP;
User
資料庫用戶名;
Password
資料庫密碼;
DataBase
數據源所要連接的資料庫;
配置完後,點擊「test」按鈕,
如果出現如下提示,即配置成功。
點擊「OK」返回用戶DSN選項卡,增加一條記錄,名為你所配置的數據源名稱。
求java連接MySQL資料庫代碼以及驅動
驅動自己搜索一下就是了,注意和MySQL版本一致。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class mysql {
/**
* @param args
*/
public static void main(String[] args) {// 多個try合併到一塊,然後使用source — format
// TODO Auto-generated method stub
//若是用到finally則需要把聲明放在try外邊
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);// 後面若是加上”.newInstance”則還需要加上幾個拋出異常
conn = DriverManager.getConnection(“jdbc:mysql://localhost/mydata?”
+ “user=rootpassword=root”);
/*
* java.sql.Statement; 不是com.mysql這個包; 二者不可以同時存在
*/
stmt = conn.createStatement();
rs = stmt.executeQuery(“select * from info”);
while (rs.next()) {
System.out.println(rs.getString(“name”));
}
// Do something with the Connection
} catch (ClassNotFoundException ex) {
// handle any errors
ex.printStackTrace();
} catch (SQLException ex) {
// TODO Auto-generated catch block
System.out.println(“SQLException: ” + ex.getMessage());
System.out.println(“SQLState: ” + ex.getSQLState());
System.out.println(“VendorError: ” + ex.getErrorCode());
} finally {
try {
if(null!= rs) {
rs.close();
rs = null;
}
if(null!= stmt) {
stmt.close();
stmt = null;
}
if(null!= conn) {
conn.close();
conn = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
mysql連接資料庫找不到合適的驅動
String url=”jdbc:mysql://localhost:3306/xueshengresult”;
下一個正確的mysql驅動
com.mysql.jdbc.Driver
這個是驅動的類型。
jdbc中需要配置不同的驅動類型來連接不同類型的資料庫,這種是連接mysql用的資料庫驅動類型。
用法舉例:class.forName(“com.mysql.jdbc.Driver”)
注意,Driver的D要大寫。
原創文章,作者:BLAP,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/139421.html