我正在Java中使用Mysql連接但使用jdbc驅動程序時遇到一些問題的小程序。我安裝了Java EE和Java SE,但我仍然收到消息,指出jdbc:mysql://localhost:3307/test
沒有合適的驅動程序。有人可以向我解釋我做錯了什麼。與java連接在java中的問題:沒有合適的驅動程序
代碼:
public class Mysql_Connection_2 {
/**
* @param args the command line arguments
*/
static String query = "select count(*) from stock";
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(MysqlConnection.class.getName()).log(Level.SEVERE, null, ex);
}
MysqlConnection.dbConnection(query);
}
}
EXTERN Connection類:
public class MysqlConnection {
private static final String dbURL = "jdbc:mysql://localhost:3307/test";
private static final String dbuname = "root";
private static final String dbpass = "usbw";
static Connection dbcon = null;
static Statement stmt = null;
static ResultSet rs = null;
public static void dbConnection (String query){
try{
//getting database connection to MySQL server
dbcon = DriverManager.getConnection(dbURL, dbuname, dbpass);
//getting PreparedStatment to execute query
stmt = dbcon.prepareStatement(query);
//Resultset returned by query
rs = stmt.executeQuery(query);
while(rs.next()) {
int count = rs.getInt(1);
System.out.println("count of stock : " + count);
}
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
}
}
你加入Oracle驅動程序jar文件 –
能否請您解釋一下在你的問題Oracle連接和MySQL連接的意義是什麼? – NewUser
是你的類路徑上的jar嗎? – hd1