Inserting data into the MySQL database in Java is empty

Source: Internet
Author: User

When inserting data into a database using Java-oriented programming. The case where the inserted data is empty. Make a summary here:

1. Database connection is normal

2.sql Statement No problem

3. Program is not reported abnormal

4. Code:

Import Java.util.Scanner;

Import Org.junit.Test;
public class Jdbctest {
2). In test method Testaaddstudent ()
1. Get the Student object entered from the console: Student student=getstudentfromconsole ();
2. Call the Addstudent (Student Stu) method to perform the insert operation

@Test
public void Testaaddstudent () {

Student Student=getstudentfromconsole ();
Addstudent (student);
}
/**
* Enter student information from the console
*/
@Test
Public Student Getstudentfromconsole () {

Scanner scanner=new Scanner (system.in);
Student student=new Student ();
System.out.println ("@Stu1" +student);
System.out.println ("Flowid:");
Student.setflowid (Scanner.nextint ());
System.out.println ("Type:");
Student.settype (Scanner.nextint ());
System.out.println ("Idcard:");
Student.setidcard (Scanner.next ());
System.out.println ("Examcard:");
Student.setexamcard (Scanner.next ());
System.out.println ("Studentname:");
Student.setstudentname (Scanner.next ());
System.out.println ("localtion:");
Student.setlocaltion (Scanner.next ());
System.out.println ("Grade:");
Student.setgrade (Scanner.nextint ());
Scanner.close ();
return student;
System.out.println ("@stu2" +student);
}

public void Addstudent (Student stu) {

//student student=new Student ();

1. Prepare an SQL statement:

String sql= "INSERT into Examstudent Values (" +stu.getflowid ()
+ "," +stu.gettype ()
+ ", '" +stu.getidcard ()
+ "', '" +stu.getexamcard ()
+ "', '" +stu.getstudentname ()
+ "', '" +stu.getlocaltion ()
+ "'," +stu.getgrade () + ")";
SYSTEM.OUT.PRINTLN (SQL);
2. Call the update (SQL) method of the Jdbctools class to perform the insert operation.
Jdbctools.update (SQL);
}
}

5. Summary: According to 1, 2, 3 judge, under normal circumstances, it is possible to add data to the database, but the added data, by viewing the database value is 0 or empty,

Later, from the logical relationship of the program to examine the analysis, found public void addstudent (Student stu) Student student=new Student (), is the reason for affecting the insertion value.

Later, the statement that creates the object in the method is stripped, and the SQL statement is written using the formal parameter, and the result is normal. An empty phenomenon that is caused by the object's re-creation.

Inserting data into the MySQL database in Java is empty

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.