Java Experiment 10-java database programming __.net

Source: Internet
Author: User

Experimental purposes:

1 Understanding the composition and structure of JDBC

2 Master the link Technology of Java program and database

3 The method of accessing the data in the database using the DriverManager class, Connection, Statement, ResultSet class provided in the java.sql package in the Java program

Experiment Request:

First set up a database, on the basis of this program to achieve the following functions:

(1) Establish a table in the database, the table is called the employee, its structure is: number, name, sex, age, salary, whether party members.

(2) Enter more than one record in the table (design specific data yourself)

(3) Increase the wages of employees over 45 by 15% and others by 10%.

(4) Display each record on the screen in the order of salary from big to small.

(5) Delete employee records with a salary exceeding 1500.

Functional Requirements:

1 The database system used is unrestricted, can be a small database system such as

MS Access,vfp,mysql or large databases, such as SQL Server.

2 use JDBC without restrictions, you can use the JDBCC-ODBC provided in J2SE

Bridge, you can also use JDBC that is dedicated to other databases.

3 display the corresponding information before and after each operation 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.microsoft.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 above 45 are increased by 15% and others by 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 over 1500 static public void Delete () throws SQLException {String sql = ' Delete from worker WHERE
    	Money > 15000 ";
    S.executeupdate (SQL);
    	///display each record in the order of the salary from large to small on the screen 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" + "sex" +sex+ "T" + "age" +age+ "T" + "wages" +money); }} public class FD {/** * @param args * @throws SQLException * @throws classnotfoundexception 
     */public static void main (string[] args) throws SQLException, ClassNotFoundException {//TODO Auto-ge
    	nerated method Stub Getdb.open ();
    	Getdb.show ();
    Getdb.close (); }
}


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.