Java Experiment 10-java Database programming

Source: Internet
Author: User

Experimental Purpose:

1) Understanding The composition and structure of JDBC

2) Mastering The link Technology of Java program and database

3) Master the DriverManager class, Connection,provided in the Java program using the java.sql Package ,Statement,ResultSet classes to access data in the database

Experimental requirements:

First set up a database. On this basis, the following functions are implemented by programming:

(1) establish a table in the database, the name of the staff, the structure of: number, name, gender, age, wages, whether party members.

(2) Enter more than one record in the table (own design detail data)

(3) add 15%to the salary of the employees who are older than above, and others add 10%.

(4) Each record is displayed on the screen in the order of the salary from the big to the small.

(5) Delete employee records with wages exceeding the limit.

Functional Requirements:

1) Use of the database system is unrestricted, can be a small database system such as

MS Access. VFP. MySql or a large database, such as SQL Server .

2) Use JDBC without restriction. Ability to use The JDBCC-ODBC provided in j2se

Other database-specific JDBCis also available.

3) before and after each operation, display the corresponding information separately to verify that the operation is completed correctly.

Package Fd.ten;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;import java.util.collection;class GetDB {static String driver = "COM.M    Icrosoft.sqlserver.jdbc.SQLServerDriver "; static String URL = "jdbc:sqlserver://localhost:1433;    DatabaseName = FD ";    static Connection con = null;    static ResultSet re = null;    static Statement s = null;            static public void Open () throws SQLException {try {class.forname (driver);            con = drivermanager.getconnection (URL, "sa", "wzz1020");        s = con.createstatement ();        } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();   }} static public void Close () throws SQLException {re.close ();   S.close ();   Con.close (); }//The wages of employees over 45 are added 15%.    Other people add 10%. static public void Update () throws SQLException {String SQL1 = "update worker SET mOney = money*1.5 WHERE age >= 50 ";    String sql2 = "UPDATE worker SET money = money*1.1 WHERE Age < 50";    S.executeupdate (SQL1);    S.executeupdate (SQL2); }//Delete employee record with payroll more than 1500 static public void Delete () throws SQLException {String sql = ' Delete from worker WHERE Mone    Y > 15000 ";    S.executeupdate (SQL);    }//Each record is displayed on the screen in the order of the wages from large to small, static public void Show () throws SQLException {String sql;    sql = "SELECT * from Dbo.worker", ORDER by Money DESC ";    Re = s.executequery (sql);    while (Re.next ()) {String name = re.getstring ("name");    String sex = re.getstring ("Sex");    String age = re.getstring ("Age"). ToString ();    String money = re.getstring ("Money"). ToString ();    System.out.println ("name" +name+ "\ T" + "gender" +sex+ "\ T" + "age" +age+ "\ T" + "salary" +money);     }}}public class FD {/** * @param args * @throws SQLException * @throws classnotfoundexception */ public static void Main (string[] args) throws SQLException, Classnotfoundexception {//TODO auto-generated Method Stub Getdb.open ();    Getdb.show ();    Getdb.close (); }}


Java Experiment 10-java Database programming

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.