【问题标题】:write data into mysql with java用java将数据写入mysql
【发布时间】:2018-05-12 18:02:01
【问题描述】:

我做了一个简单的类来测试天气我是否能够将数据写入我的数据库

package climaxinvoice;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;

public class InsertValues {

    public static void main(String args[]) throws ClassNotFoundException, SQLException
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/cxinvoice.invoice_list","admin","admin");
            Calendar cdr = Calendar.getInstance();
            java.sql.Date startDate = new java.sql.Date(cdr.getTime().getTime());
            String query = " insert into cxinvoice.invoice_list (number, company, date, payment, quantity, subtotal_five, cgst_five, sgst_five, subtotal_twelve, cgst_twelve, sgst_twelve, total, items)" + " value (?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement st = conn.prepareStatement(query);
            st.setInt(1,1);
            st.setString(2,"Climax Exclusive");
            st.setDate(3,startDate);
            st.setString(4,"CASH");
            st.setString(5,"5");
            st.setString(6,"1000");
            st.setString(7,"250");
            st.setString(8,"250");
            st.setString(9,"2000");
            st.setString(10,"750");
            st.setString(11,"750");
            st.setString(12,"3000");
            st.setString(13,"a|b|c|d");
            st.execute();
            conn.close();
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

文件运行,但我无法看到我的表格有任何更改 我是 MySQL 新手 附上我的工作台的屏幕截图 workbench output

【问题讨论】:

  • 自动提交为假或真。看起来它是假的..你能明确地调用connection.commit(),让我们知道会发生什么吗?应该可以解决这个问题
  • @user641887 它没有帮助
  • 您在控制台中看到了什么?
  • 没有错误构建成功只是我在我的数据库中看到空表

标签: java mysql prepared-statement mysql-workbench


【解决方案1】:

一般autocommit默认为true。

如果不使用:

con.autocommit(true)

con.commit()在交易结束时

但我认为这不会完美执行:

更新 SQL 查询中的拼写错误 VALUES 而不是 VALUE。确保没有其他错别字。

插入 invoice_list(编号、公司、日期、付款、 数量,subtotal_five,cgst_five,sgst_five,subtotal_twelve, cgst_twelve、sgst_twelve、总计、项目)" + " (?,?,?,?,?,?,?,?,?,?,?,?,?)";

【讨论】:

  • 你尝试了什么?
  • 都在我的代码的末尾 st.setString(13,"a|b|c|d"); st.execute(); conn.commit(); conn.close();
  • 你改错了吗?
  • 任何错误信息?描述你的数据库表>
  • 也描述你的数据库表
【解决方案2】:

不要使用日历类,而是像这样使用日期类和 SimpledDateFormatter 类,然后进行插入。

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    Date date = new Date();
    System.out.println(dateFormat.format(date));

【讨论】:

  • 在getConnection中删除表名只保留数据库名,在查询字符串中删除数据库名只保留表名你的代码有很多错误
  • 我尝试更正数据库名称,但之后也没有用
  • 我需要在数据库中添加日期
  • 将值保留在与插入相同的双引号内为什么你用 + 连接它
  • 我也试过日期代码还是不行
【解决方案3】:

将连接字符串从“jdbc:mysql://localhost:3306/cxinvoice.invoice_list”更改为“jdbc:mysql://localhost:3306/cxinvoice”

另外, 您的插入语句不正确。将“值”更改为“值”。

    Insert into cxinvoice.invoice_list (number, company, date, payment, quantity, subtotal_five, cgst_five, sgst_five, subtotal_twelve, cgst_twelve, sgst_twelve, total, items)" + " values(?,?,?,?,?,?,?,?,?,?,?,?,?)";

【讨论】:

  • @sanjukgupta,你能接受这个作为正确答案吗?
  • 即使在纠正错字后我的问题也没有解决空表
  • @sanjulgupta,您需要更正您的连接字符串。它肯定会起作用。
【解决方案4】:

构建成功

“构建成功”仅表示您构建了它。你还没有执行这个根本。如果你有,你会因为 VALUE 处的 SQL 语法错误而得到一个异常。

【讨论】:

  • 是的,你是对的,我点击运行文件如何纠正这个问题?
【解决方案5】:

抱歉给你添麻烦了 我猜驱动程序有问题 我将 JAR for MySQL 连接器添加到我的库中 它开始工作 谢谢大家的帮助 这个问题可以关闭 再次感谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 2019-11-17
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    相关资源
    最近更新 更多