如何与数据库建立连接

时间:2013-07-20 06:35:37

标签: java mysql eclipse swing

我在mysql.Now中创建了一个数据库,试图将数据库连接到程序,但我不能。这是我的代码的一部分

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)。我不知道什么是真正的问题是????请帮忙

1 个答案:

答案 0 :(得分:4)

您错过了getconnection()中的端口号

变化

con=DriverManager.getConnection("jdbc:mysql://localhost/hr","abilash","abilash");

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","abilash","abilash");