使用jdbc

时间:2015-07-29 05:06:48

标签: java mysql jdbc

在数据库中看不到使用程序插入的值。以下是我用于相同

的代码
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class JDBC {

    private static Scanner sc;

    public static void main(String args[]) throws Exception {

        sc = new Scanner(System.in);
        System.out.println("hii");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/rsystems", "root", "rsystems");
        PreparedStatement stmt = con.prepareStatement("insert into user valiues(?,?)");
        System.out.println("Enter id:");
        int no = sc.nextInt();
        System.out.println("Enter name:");
        String name = sc.next();
        stmt.setInt(1, no);
        stmt.setString(2, name);
        //I think I need to commit over here and I have tried the same but even with that I didn't see my inserted data in database
    }

}

7 个答案:

答案 0 :(得分:0)

您的PreparedStatement

中的查询错误
PreparedStatement stmt = con.prepareStatement("insert into user (id, name) values(?,?)");

答案 1 :(得分:0)

您的查询错误,您永远不会执行它。

PreparedStatement stmt = con.prepareStatement("insert into user values(?,?)");
stmt.execute();

答案 2 :(得分:0)

您没有在preparedStatement中执行查询。还要确保你有有效的SQL。

您可以使用PreparedStatement.executeUpdate()

  

在此PreparedStatement对象中执行SQL语句,该对象必须是SQL数据操作语言(DML)语句,例如INSERT,UPDATE或DELETE;或者什么都不返回的SQL语句,例如DDL语句。

尝试

PreparedStatement stmt = con.prepareStatement("insert into user values(?,?)");
stmt.executeUpdate();

从维护的角度来看,我们在插入查询中指定列的顺序,如。

insert into user (id, name) values(?,?)

<强>提交:

您可以检查有关autoCommit的数据库配置。如果默认情况下autoCommit为true,那么您可以省略代码中的提交,否则您必须在代码中手动提交。像

con.commit();

关闭关闭:

您应该在不再需要时关闭语句和连接。

stmt.close();
connection.close();

注意:不要忘记处理/抛出所有这些方法的异常抛出,例如SQLException

答案 3 :(得分:0)

也许你可以使用try..catch帮助你找到你犯错误的错误

答案 4 :(得分:0)

 public static void main(String args[]) {

        String dbURL = "jdbc:mysql://localhost:3306/student";
        String username = "root";
        String password = "root";
        try {
            Connection conn = DriverManager.getConnection(dbURL, username, password);

            if (conn != null) {
                System.out.println("Connected");
            }

            String sql = "INSERT INTO Users (id, password,) VALUES (?, ?)";

            PreparedStatement statement;
            statement = conn.prepareStatement(sql);
            statement.setInt(1, 1);
            statement.setString(2, "admin");

            int rowsInserted = statement.executeUpdate();
            if (rowsInserted > 0) {
                System.out.println("A Record inserted successfully!");
            }
        } catch (SQLException e) {
            System.out.println("Exception occured" + e);
        }
    }

答案 5 :(得分:0)

设置值后需要执行PreparedStatement 请看下面的

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class JDBC {

    private static Scanner sc;

    public static void main(String args[]) throws Exception {

        sc = new Scanner(System.in);
        System.out.println("hii");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/rsystems", "root", "rsystems");
        PreparedStatement stmt = con
                .prepareStatement("insert into user valiues(?,?)");
        System.out.println("Enter id:");
        int no = sc.nextInt();
        System.out.println("Enter name:");
        String name = sc.next();
        stmt.setInt(1, no);
        stmt.setString(2, name);
        stmt.execute();//You have to execute this statement
        // I think I need to commit over here and I have tried the same but even
        // with that I didn't see my inserted data in database
    }

}

答案 6 :(得分:0)

这有效:

import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class JDBC {

private static Scanner sc;

public static void main(String args[]) throws Exception {

    sc = new Scanner(System.in);
    System.out.println("hii");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/rsystems", "root", "rsystems");
    PreparedStatement stmt = con.prepareStatement("insert into user values(?,?)");
    System.out.println("Enter id:");
    int no = sc.nextInt();
    System.out.println("Enter name:");
    String name = sc.next();
    stmt.setInt(1, no);
    stmt.setString(2, name);
    stmt.executeUpdate();
}

}