Find, delete, and insert information into the database (SQL2008) through the My Eclipse console

Source: Internet
Author: User
Tags sql 2008

If there's anything wrong with compiling the program, I hope you'll correct me.

The database, rack package, and Java source files required for code execution are uploaded to the file filename sql_jdbc.zip


Before you use the My Eclipse console to manipulate the database (SQL 2008), you should introduce a rack package (Sqljdbc4.jar) before the rack package is imported to operate the database through the console
Com.microsoft.sqlserver.jdbc.SQLServerDriver  Drivers After the introduction of the Rack Pack (Sqljdbc4.jar)
Jdbc:sqlserver://localhost:1433;databasename=pet "," sa "," 123 "    

Fixed notation Local data 1433 represents SQL data port databaseName= database name SA login password for SQL database login 123 for SQL database
Package Jdbchomework;import java.util.scanner;//Test class file public class Test {//Main program entry public static void Main (string[] Ar        GS) {//Instantiate keyboard input Scanner input=new Scanner (system.in);        Instantiate the Operation class do a=new do ();        System.out.print ("Please enter user name:");        String Username=input.next ();        System.out.print ("Please enter password");        String Password=input.next (); if (A.found (username, password)) {//Enter user name and password to compare with username and password in database System.out.println ("Login Successful! You are welcome! "+username"); for (;;) {
          System.out.println ("Select operation:") System.out.println ("1. Query Administrator Information 2. Search for dog information 3. Add a dog 4. Delete Dog 0. Exit"); System.out.print ("Please select:"); int Num=input.nextint (); Switch (num) {case 1:a.show (); Invoke the Operation class to display the owner information break; Case 2:a.show1 (); Call action class to show the dog information break; Case 3:system.out.print ("Please enter the name of the Dog:"); String Name=input.next (); System.out.print ("Please enter the dog's health Value:"); int Health=input.nextint (); System.out.print ("Please enter the intimacy with the dog:"); int Love=input.nextint (); System.out.print ("Please enter the breed of dog:"); String Strain=input.next (); A.SHOW2 (name, health, love, strain); Invoking an action class to insert data into the database dog insert data Break Case 4:system.out.print ("Please enter Number:"); int Num1=input.nextint (); A.SHOW3 (NUM1); Call the action class to delete data from the delete database in the dog table with ID NUM1 data break; Case 0:num=0; Break DEFAULT:SYSTEM.OUT.PRINTLN ("input error"); Break } if (num==0) {System.out.println ("system exit, thank you for using!!! "); Break }}}else{System.out.println ("User name or password error"); } }}
Package Jdbchomework;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import Java.sql.resultset;import Java.sql.sqlexception;import Java.util.Scanner;
Operation class file public class do {//Login interface public boolean found (String username,string password) {Boolean find=false; Load Drive try {//Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver "); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }//Establish connection Connection cn=null; Declares the connection object PreparedStatement Ps=null; Operation Object ResultSet Rs=null; Result set Object try {//cn=drivermanager.getconnection ("Jdbc:sqlserver://localhos T:1433;databasename=pet "," sa "," 123 "); String sql= "SELECT * from Admin where username= '" +username+ "' and pwd= '" +password+ "'"; String sql= "SELECT * from Admin where username=? and pwd=? "; Ps=cn.preparestatement (SQL); Ps.setstring (1, username); Ps.setstring (2, password); Rs=ps.executequery (); if (RS.NExt ()) {find=true; }else{Find=false; }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } try{if (ps!=null) {ps.close (); } if (Cn!=null) {cn.close (); }}catch (SQLException e) {e.printstacktrace (); } return find; }//Show host information public void Show () {try {class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriv Er "); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }//Establish connection Connection cn=null; PreparedStatement Ps=null; ResultSet Rs=null; try {cn=drivermanager.getconnection ("Jdbc:sqlserver://localhost:1433;databasename=pet", "sa", "123"); String sql= "SELECT * from Admin where username= '" +Username+ "' and pwd= '" +password+ ""; String sql= "SELECT * from Admin"; Ps=cn.preparestatement (SQL); Rs=ps.executequery (); SYSTEM.OUT.PRINTLN ("Host information list"); System.out.println ("the number \ t name \ t ingot number"); while (Rs.next ()) {int num=rs.getint (1); String name=rs.getstring (2); int Money=rs.getint (3); System.out.println (num+ "\ t" +name+ "\ T" +money); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } try{if (rs!=null) {rs.close (); } if (Ps!=null) {ps.close (); } if (Cn!=null) {cn.close (); }}catch (SQLException e) {e.printstacktrace (); }}//show dog Information public void Show1 () {try {class.forname ("com.microsoft.sqlserver. Jdbc. Sql serverDriver "); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }//Establish connection Connection cn=null; PreparedStatement Ps=null; ResultSet Rs=null; try {cn=drivermanager.getconnection ("Jdbc:sqlserver://localhost:1433;databasename=pet", "sa", "123"); String sql= "SELECT * from Admin where username= '" +username+ "' and pwd= '" +password+ "'"; String sql= "SELECT * from Dog"; Ps=cn.preparestatement (SQL); Rs=ps.executequery (); System.out.println ("Dog Information list"); System.out.println ("t \ n name \ t health value \ t intimacy t variety"); while (Rs.next ()) {int num=rs.getint (1); String name=rs.getstring (2); int Health=rs.getint (3); int Love=rs.getint (4); String Strain=rs.getstrING (5); System.out.println (num+ "\ t" +name+ "\ T" +health+ "\ T" +love+ "\ T" +strain); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } try{if (rs!=null) {rs.close (); } if (Ps!=null) {ps.close (); } if (Cn!=null) {cn.close (); }}catch (SQLException e) {e.printstacktrace (); }}//Insert data public void Show2 (string Name,int health,int love, string strain) {try {Class.forn Ame ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); } Connection Cn=null; PreparedStatement Ps=null; try {cn=drivermanager.getconnection("Jdbc:sqlserver://localhost:1433;databasename=pet", "sa", "123"); String sql= "Insert Dog values (?,?,?,?)"; Ps=cn.preparestatement (SQL); Ps.setstring (1, name); Ps.setint (2, health); Ps.setint (3, Love); Ps.setstring (4, strain); int i=ps.executeupdate (); if (i>0) {System.out.println ("Congratulations, add success!!! "); }else{System.out.println ("Add failed!!! "); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } try {if (ps!=null) {ps.close (); } if (Cn!=null) {cn.close (); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }}//delete data public void show3 (int num) {//Load Drive try {class.forname ("Com.microsoft.sqlserv Er.jdbc.sQlserverdriver "); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); } Connection Cn=null; PreparedStatement Ps=null; try {cn=drivermanager.getconnection ("Jdbc:sqlserver://localhost:1433;databasename=pet", "sa", "123"); String sql= "Delete Dog where id=?"; Ps=cn.preparestatement (SQL); Ps.setint (1, NUM); int i=ps.executeupdate (); if (i>0) {System.out.println ("Congratulations, delete success!!! "); }else{System.out.println ("Delete failed, you entered the ID does not exist!!!" "); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }//Release connection try {if (ps!=null) {ps.close (); } if (Cn!=null) {cn.close (); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } }}




Find, delete, and insert information into the database (SQL2008) through the My Eclipse console

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.