Java 将 netbeans 中的值插入到 mysql 数据库表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20730453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert values from netbeans into mysql database table
提问by DJerome
I am making a standalone database application in which I use netbeans for java and a mysql database as my server.
我正在制作一个独立的数据库应用程序,其中我使用 netbeans for java 和一个 mysql 数据库作为我的服务器。
Inserting basic values into my database tables fails with an exception "no jdbc driver found for jdbc:mysql:\localhost\basicinfo" where in basicinfo is my database name with "info" as my database table. My code:
将基本值插入我的数据库表失败,出现异常“没有找到 jdbc:mysql:\localhost\basicinfo 的 jdbc 驱动程序”,其中 basicinfo 是我的数据库名称,“info”作为我的数据库表。我的代码:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class class1
{
public static void main(String[] args )
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql:\localhost\basicinfo","root","root");
Statement stmt=(Statement)con.createStatement();
String name="Jerome Dcruz";
String contactno="9773523568";
String insert="INSERT INTO info VALUES('"+name+"','"+contactno+"');";
stmt.executeUpdate(insert);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage() ,"Error", 1);
}
}
}
回答by Muhammad
you should have the mysql-connector-java driver int C:\Program Files\Java\jdk1.7.0_25\jre\lib\ext
你应该有 mysql-connector-java 驱动程序 int C:\Program Files\Java\jdk1.7.0_25\jre\lib\ext
and also place the port number of your database like the following
并放置数据库的端口号,如下所示
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/basicinfo", "root", "root");
you can download the driver from here
你可以从这里下载驱动程序
回答by KNU
Error Found at (line #15) :
在(第 15 行)处发现错误:
Connection con = DriverManager.getConnection("jdbc:mysql:\localhost\basicinfo", "root", "root");
Correct it to :
更正为:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/basicinfo", "root", "root");
Conclusion : There was an error('\' instead of '/') in path and as such, it was impossible to find the specified schema and table.
结论:路径中存在错误('\' 而不是 '/'),因此无法找到指定的架构和表。