Java Netbeans MySql 数据库连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21257977/
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
Java Netbeans MySql database Connection
提问by user3219118
I want to insert row into MySql database with a normal Program for Mysql connection in Java Netbeans,but when i run this code my database remains unaffected.I had setup connection with Netbeans and Mysql and it is working fine.
我想在 Java Netbeans 中使用普通的 Mysql 连接程序将行插入 MySql 数据库,但是当我运行此代码时,我的数据库不受影响。我已经设置了与 Netbeans 和 Mysql 的连接,并且工作正常。
Code:
代码:
import java.sql.*;
public class MySqlConnection {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/migration";
static final String USER = "root";
static final String PASS = "ngts12345";
public static void main(String[] args) {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
System.out.println("Inserting records into the table...");
String sql = "INSERT INTO document (document_id, document_name, format)" +
"VALUES (?, ?, ?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, "Test2");
preparedStatement.setString(3, "Test3");
preparedStatement.executeUpdate();
preparedStatement.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
}catch(Exception e){
//Handle errors for Class.forName
}
}
}
回答by SpringLearner
there is not space after format)
and values
format)
和之后没有空格values
Change this line to
将此行更改为
String sql = "INSERT INTO document (document_id, document_name, format)" +
"VALUES (?, ?, ?)";
to
到
String sql = "INSERT INTO document(document_id, document_name, format) " +
"VALUES (?, ?, ?)";
回答by user2810910
Note preparedStatement.executeUpdate() returns true (record was successful in insert, update, or delete) or false (not successful). Therefore I suggest printing out the result to see if it worked.
As for not getting an error message, are you consuming exceptions and not printing out the error? In your catch block, put e.printStackTrace() to see what the error is.
注意preparedStatement.executeUpdate() 返回true(插入、更新或删除记录成功)或false(不成功)。因此,我建议打印出结果,看看它是否有效。
至于没有收到错误消息,你是在消费异常而不是打印出错误吗?在您的 catch 块中,放入 e.printStackTrace() 以查看错误是什么。
回答by chinkx
Try doing this:
尝试这样做:
preparedStatement.executeUpdate(sql);
and you should also specify database
你还应该指定数据库
String sql = "INSERT INTO database_name.document (document_id, document_name, format)" +"VALUES (?, ?, ?)";
回答by chinkx
enter code here
try{
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306","root","");
String sql = "select * from 'table_name'.database_name where user=? and pass=?";
pst = conn.prepareStatement(sql);
rs= pst.executeQuery();}