无法使用Java连接到MySQL
我正在使用下面的代码连接到MySQL无法使用Java连接到MySQL,java,mysql,jdbc,mysql-workbench,Java,Mysql,Jdbc,Mysql Workbench,我正在使用下面的代码连接到MySQL import java.sql.DriverManager; import com.mysql.jdbc.Connection; public class connectMysql { public static void main(String[] args){ Connection conn = null; try{ conn = (Connection)DriverManager.ge
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
public class connectMysql {
public static void main(String[] args){
Connection conn = null;
try{
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306//test","root","admin");
if(conn!=null)
{
System.out.println("Connected successfully");
}
}catch(Exception e)
{
System.out.println("Not connected");
e.printStackTrace();
}
}
}
用户名:root,密码:admin,主机名:localhost,端口:3306。
我得到的输出为“未连接”
[编辑]现在我堆叠跟踪,并看到错误为“未知数据库”/test。但我有一个名为test的模式。架构是否与数据库相同
一切都是这样。但我似乎没有连接到数据库。
谢谢你的帮助 检查库中的所有驱动程序是否可用 检查端口号、用户名和密码(区分大小写) 如果一切都是正确的话 使用下面的代码
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
statement = con.createStatement();
检查库中的所有驱动程序是否可用 检查端口号、用户名和密码(区分大小写) 如果一切都是正确的话 使用下面的代码
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
statement = con.createStatement();
为什么在连接字符串中的数据库名称前面有双斜杠//呢
jdbc:mysql://localhost:3306//test","root","admin"
换成
jdbc:mysql://localhost:3306/test","root","admin"
为什么在连接字符串中的数据库名称前面有双斜杠//呢
jdbc:mysql://localhost:3306//test","root","admin"
换成
jdbc:mysql://localhost:3306/test","root","admin"
请在例外块中打印“e”。它会给你一个惊喜hint@Tammy谢谢你。我添加了它,得到了我在描述中提到的错误。@Josephine不需要在文本jdbc前面加双斜杠:mysql://localhost:3306/test“@塔米,谢谢。现在可以了。双斜杠是问题所在。请在异常块中打印“e”。它会给你一个惊喜hint@Tammy谢谢你。我添加了它,得到了我在描述中提到的错误。@Josephine不需要在文本jdbc前面加双斜杠:mysql://localhost:3306/test“@Tammy谢谢。它现在可以工作了。双斜杠是问题所在。我添加了您的代码,并在控制台中获得了此输出:“线程中的异常”main”java.lang.Error:未解决的编译问题:无法将语句解析为变量connect无法在connectMysql.main(connectMysql.java:10)”解析@Josephine抱歉其con not connect将最后一行更改为statement=con.createStatement(),我添加了代码并在控制台中获得此输出:“线程中的异常”mainjava.lang.Error:未解决的编译问题:无法将语句解析为变量connect无法在connectMysql.main(connectMysql.java:10)”解析@Josephine抱歉其con not connect将最后一行更改为statement=con.createStatement()