我无法建立连接,我不知道为什么,它显示:
java.sql.SQLException:找不到合适的驱动程序 jbdc://localhost:3306/rede_hoteis 在 java.sql/java.sql.DriverManager.getConnection(DriverManager.java:708) 在 java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230) 在 conexao.Conexao.getConexao(Conexao.java:16) 在 DAO.itens_lavanderiaDAO.cadastraItem(itens_lavanderiaDAO.java:15)
在 App.main(App.java:14)
代码如下:
App.java:
import DAO.itens_lavanderiaDAO;
import entitys.itens_lavanderia;
public class App {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
itens_lavanderia item1 = new itens_lavanderia();
item1.setDescricao("lavagemzinha");
item1.setUnidade(2);
item1.setValor(21.5);
new itens_lavanderiaDAO().cadastraItem(item1);
}
}
Conexao.java:
package conexao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conexao{
private static final String url = "jbdc://localhost:3306/rede_hoteis";
private static final String user = "root";
private static final String password = "1234";
private static Connection conn;
public static Connection getConexao(){
try {
if(conn == null){
conn = DriverManager.getConnection(url, user, password);
}
return conn;
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}
我也尝试删除
Class.forName("com.mysql.cj.jdbc.Driver");
Itens_lavanderia.java:
package DAO;
import java.sql.PreparedStatement;
import conexao.Conexao;
import entitys.itens_lavanderia;
public class itens_lavanderiaDAO {
public void cadastraItem(itens_lavanderia item){
String sql = "INSERT INTO itens_lavanderia (descricao, valor_unitario, unidade) VALUES ( ?, ?, ?)";
PreparedStatement ps = null;
try {
ps = Conexao.getConexao().prepareStatement(sql);
ps.setString(1, item.getDescricao());
ps.setDouble(2, item.getValor());
ps.setInt(3, item.getUnidade());
ps.execute();
ps.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
MySQL JDBC 数据库 URL 需要采用以下格式:
jdbc:mysql://<dbhost>:<dbport>/<dbname>
(请参阅 https://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html)
所以我想你需要将你的
url
更改为:
private static final String url = "jdbc:mysql://localhost:3306/rede_hoteis";