你如何用Java连接到MySQL数据库?
当我尝试时,我明白了
java.sql.SQLException: No suitable driver found for jdbc:mysql://database/table
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
Run Code Online (Sandbox Code Playgroud)
要么
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)
要么
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
Run Code Online (Sandbox Code Playgroud) Class.forName()
和之间有什么区别Class.forName().newInstance()
?
我不明白这个显着的区别(我已经读过一些关于它们的东西!).请你帮助我好吗?
我正在尝试将已启用数据库的JSP添加到现有的Tomcat 5.5应用程序(GeoServer 2.0.0,如果有帮助的话).
该应用程序本身与Postgres谈话很好,所以我知道数据库已启动,用户可以访问它,所有这些好东西.我想要做的是在我添加的JSP中的数据库查询.我已经在Tomcat数据源示例中使用了配置示例,非常开箱即用.必要的taglibs位于正确的位置 - 如果我只有taglib refs就不会发生错误,因此它会找到那些JAR.postgres jdbc驱动程序postgresql-8.4.701.jdbc3.jar位于$ CATALINA_HOME/common/lib中.
这是JSP的顶部:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/mmas">
select current_validstart as ValidTime from runoff_forecast_valid_time
</sql:query>
Run Code Online (Sandbox Code Playgroud)
来自$ CATALINA_HOME/conf/server.xml的相关部分,其内部<Host>
依次是<Engine>
:
<Context path="/gs2" allowLinking="true">
<Resource name="jdbc/mmas" type="javax.sql.Datasource"
auth="Container" driverClassName="org.postgresql.Driver"
maxActive="100" maxIdle="30" maxWait="10000"
username="mmas" password="very_secure_yess_precious!"
url="jdbc:postgresql//localhost:5432/mmas" />
</Context>
Run Code Online (Sandbox Code Playgroud)
这些行是webapps/gs2/WEB-INF/web.xml中标记的最后一行:
<resource-ref>
<description>
The database resource for the MMAS PostGIS database
</description>
<res-ref-name>
jdbc/mmas
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
Run Code Online (Sandbox Code Playgroud)
最后,例外:
exception
org.apache.jasper.JasperException: Unable …
Run Code Online (Sandbox Code Playgroud) 在尝试 JDBC 程序使用 eclipse 连接到 PostgreSQL 数据库时,它标记了一个错误:
java.sql.SQLException: No suitable driver found
Run Code Online (Sandbox Code Playgroud)
建议将 PostgreSQL 驱动程序 jar 文件放在类路径上。现在我的问题是,如何将文件放在类路径上?
我是 eclipse 的新手,所以最好有详细的解释。
我有一个网站,我在该网站上也有一个MySQL数据库.我可以从phpMyAdmin在线处理我的数据库,它的名字是"dbPersons";
但是我想从Java连接到该数据库.我已经下载了JDBC,但由于我的连接字符串,我无法连接.我使用以下连接字符串
String mySQLCredecials = "jdbc:mysql:thin:" + user + "/" + pass + "@jdbc:mysql:www.websiteName.net:2222/dbPersons";
Run Code Online (Sandbox Code Playgroud)
你知道我做错了什么吗?