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, establish a database, on this basis by programming to achieve the following functions:
(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 specific data)
(3) Increase the salary of the employees who are older than 15%and Increase the 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 large databases, such as SQL Server .
2) using JDBC is unrestricted, you can use The JDBCC-ODBC provided in j2se
Bridge, or you can use JDBCthat is dedicated to other databases.
3) Display the appropriate 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.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 increase by 15%, others increase 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 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 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