Working with my Exclipse database (2)

Source: Internet
Author: User
Tags stmt

Second, increase

public static void Main (string[] args) {
TODO auto-generated Method Stub

4. User input items that need to be added
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter the data you want to add:");
System.out.println ("2. Name");
String a = Sc.nextline ();
System.out.println ("3. Gender");
String three = Sc.nextline ();
System.out.println ("4. Age");
int fore= integer.parseint (Sc.nextline ());
System.out.println ("5. School Code");
int five= integer.parseint (Sc.nextline ());

try {

1. Import the database and link

Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");

Connection conn = drivermanager.getconnection ("Jdbc:sqlserver://192.168.8.60;databasename=school", "sa", "123456");

3. Write SQL statements

String sql= "INSERT into [Student] values (? ,? ,? ,? )";

2. Remove the database content and show it in SQL form

PreparedStatement stmt= conn.preparestatement (SQL);

Stmt.setobject (1,two);

Stmt.setobject (2,three);

Stmt.setobject (3,fore);

Stmt.setobject (4,five);

5. Decide whether to add success

if (!stmt.execute ()) {

SYSTEM.OUT.PRINTLN ("Data added successfully");

}else{

SYSTEM.OUT.PRINTLN ("Data addition not successful");

}

6. Close the data

if (!stmt.isclosed ()) {stmt.close ();     } if (!conn.isclosed ()) {conn.close (); }

} catch (ClassNotFoundException E1) {//TODO auto-generated catch block E1.printstacktrace ();    }catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }                   }

Working with my Exclipse database (2)

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.