Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用Java/JDBC在sqLite中插入数据_Java_Sqlite_Jdbc - Fatal编程技术网

不使用Java/JDBC在sqLite中插入数据

不使用Java/JDBC在sqLite中插入数据,java,sqlite,jdbc,Java,Sqlite,Jdbc,我有两个Jtextfield和一个按钮。jtextfield的名称是Jtextfield1和Jtextfield2。我已经尝试了以下代码。我无法使用netbeans在sqLite(Mozilla Firefox)中插入数据 Statement st; try { st=con.createStatement(); st.executeUpdate("insert into emp(name,des) values ("+jTextField1.getText()+","+jText

我有两个Jtextfield和一个按钮。jtextfield的名称是Jtextfield1和Jtextfield2。我已经尝试了以下代码。我无法使用netbeans在sqLite(Mozilla Firefox)中插入数据

Statement st;

try {
   st=con.createStatement();
   st.executeUpdate("insert into emp(name,des) values ("+jTextField1.getText()+","+jTextField2.getText()+")");
   JOptionPane.showMessageDialog(null, "pass");
   st.close();
   con.close();
} catch (Exception e) {
   JOptionPane.showMessageDialog(null, "fail");
}

我猜您需要用单引号(“”)封装字符串:

要了解有关错误的更多信息,应将异常打印到控制台,如下所示:

[...]
} catch (Exception e) {
   System.out.println("Exception: " + e) ;
[...]

也许这将帮助您了解问题所在。

@user5809644我正在遵循系统中运行的以下步骤。请按照说明和代码操作。我想你会得到结果的。您需要下载MySQL社区服务器和MySQL WorkbenchHand并安装(如果不需要下载并安装)

然后转到netbeans右键单击库选择“添加Jar/文件夹”,然后会出现一个窗口。选择netbeans文件夹(例如:C:\Program Files\netbeans 8.0.2\ide\modules\ext)选择mysql-connector-java5.1.23-bin.jar文件。成功安装后,您可以将jar文件保存到libraries文件夹中

然后转到服务(窗口>>服务),您可以看到数据库。右键单击数据库,选择新建连接。一个窗口将显示为“新建连接向导”的名称。从下拉列表中选择mysql(连接器/J驱动程序)并添加我上面提到的已经添加的jar文件(*(示例:C:\Program Files\NetBeans 8.0.2\ide\modules\ext)选择mysql-connector-java5.1.23-bin.jar文件。*命名驱动程序。单击next>>可以看到驱动程序名称、主机、数据库、用户名、密码(请注意,如果MySQL中提供了密码,请在此处输入密码。) 在JDBC url中,您可以看到JDBC:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull然后删除*?zeroDateTimeBehavior=ConverttoFull*。然后单击“测试连接””按钮,您将获得“成功”结果。然后单击下一步,然后完成该过程

然后,您可以在服务>>数据库>>jdbc中看到:mysql://localhost:3306/mysql.

双击它可以看到一个数据库名“mysql”

单击它,然后在表文件夹中创建表。(这里我创建了一个表名“emp”,并创建了一些列名,如名称、地址)。您还可以创建自己的表和表列

转到JForm选择源,然后输入下面的代码

 import java.sql.*;
import javax.swing.*;

import javax.swing.JOptionPane;

public class Demoinsert extends javax.swing.JFrame {

    Connection con= null;

    Statement st= null;

    PreparedStatement pt= null;
    /**
     * Creates new form Demoinsert
     */
    public Demoinsert() {
        initComponents();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");                
            jLabel3.setText("sucess");
        } catch (Exception e) {

            System.out.println("  "+e);
            jLabel3.setText("fail");
        }


    }


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        try {
            String sql="insert into emp(Ename,Designation) values(?,?)";
           pt= con.prepareStatement(sql);
          pt.setString(1, jTextField1.getText());
            pt.setString(2, jTextField2.getText());
            jLabel3.setText("sucess");

            pt.execute();


        } catch (Exception e) {
            System.out.println("  "+e);
             jLabel3.setText("fail");

        }

    }        

你有任何异常吗?@Berger我得到的结果是“失败”,如果你不处理异常怎么办,这样你就能真正看到出了什么问题?是的,添加e.printStackTrace()来知道发生了什么。@a_horse_,没有名字,我试过PreparedStatement。但在异常中显示相同的错误“Exceptionjava.lang.NullPointerException”,它显示为:“Exceptionjava.lang.NullPointerException”连接“con”对象来自哪里?con表示与数据库的连接名称。“connection con=null;”不能将其设置为null。您需要将其设置为有效的数据库连接。请参阅我添加了sqlite管理器并创建了数据库名称“company”,我还创建了一个表“emp”,然后我只需复制文件“company.sqlite”并粘贴到应用程序文件夹中。并给出以下代码:Class.forName(“org.sqlite.JDBC”);Connection con=DriverManager.getConnection(“JDBC:sqlite:company.sqlite”、“”、“”);Statement st=con.createStatement();JOptionPane.showMessageDialog(null,“pass”);}catch(异常e){JOptionPane.showMessageDialog(null,“fail”);}在此代码中,con=drivermanager.getConnection(“”)是基于JDBC url的链接提出来的。(connectionlink/databasename,user id,password)。如果密码为空,则提出来作为“”只有。不要输入任何东西。这是我的数据库用户名和密码是root。也就是说,我在那个地方提到了用户id,密码是“root”,“root”。
 import java.sql.*;
import javax.swing.*;

import javax.swing.JOptionPane;

public class Demoinsert extends javax.swing.JFrame {

    Connection con= null;

    Statement st= null;

    PreparedStatement pt= null;
    /**
     * Creates new form Demoinsert
     */
    public Demoinsert() {
        initComponents();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");                
            jLabel3.setText("sucess");
        } catch (Exception e) {

            System.out.println("  "+e);
            jLabel3.setText("fail");
        }


    }


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        try {
            String sql="insert into emp(Ename,Designation) values(?,?)";
           pt= con.prepareStatement(sql);
          pt.setString(1, jTextField1.getText());
            pt.setString(2, jTextField2.getText());
            jLabel3.setText("sucess");

            pt.execute();


        } catch (Exception e) {
            System.out.println("  "+e);
             jLabel3.setText("fail");

        }

    }