我已经在 mysql 中创建了一个数据库。现在尝试将数据库连接到程序,但是我不能。这是我的代码的一部分
Class.forName("com.mysql.jdbc.driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/hr","abilash","abilash");
System.out.println("connected");
java.sql.Statement stmt = con.createStatement();
System.out.println("Connected Successfully");
stmt.executeUpdate("insert into emp (Name) values( '"+fnamet.getText()+"') ");
JOptionPane.showMessageDialog(null,"Item Successfully Added",
"Confirmation",JOptionPane.INFORMATION_MESSAGE);
我正在使用 eclipse,我已经添加了 mysql-connector-java.zip(5.1.25)。我不知道真正的问题是什么????请帮忙
最佳答案
你在 getconnection() 中错过了端口号
改变
con=DriverManager.getConnection("jdbc:mysql://localhost/hr","abilash","abilash");
到
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","abilash","abilash");
关于java - 如何与数据库建立连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759466/