Java experiment 10-java database programming, 10-java Database Programming

Source: Internet
Author: User

Java experiment 10-java database programming, 10-java Database Programming

Purpose:

1) understand the composition and structure of JDBC

2) master the connection technology between Java programs and databases

3) Master how to use the DriverManager class, Connection, Statement, and ResultSet classes provided in the Java. SQL package to access data in the database.

Lab requirements:

First, establish a database and implement the following functions through programming:

(1) create a table in the database named "employee". The table structure is number, name, gender, age, salary, and whether the table is a member of the staff.

(2) Input Multiple records in the table (design specific data by yourself)

(3) increase the salary of employees older than 45 by 15% and that of other employees by 10%.

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

(5) delete employee records with wages exceeding 1500.

Functional requirements:

1) The database system used is unrestricted. It can be a small database system, such

MS Access, VFP, MySql or large databases, such as SQL server.

2) use JDBC is unrestricted and can use the JDBCC-ODBC provided in J2SE

You can also use JDBC for other databases.

3) the corresponding information is displayed before and after each operation to verify whether 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; sta Tic 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 salary of employees older than 45 increases by 15%, while that of others increases 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.exe cuteUpdate (sql1); s.exe cuteUpdate (sql2 );} // delete the static public void DELETE () throws SQLException {String SQL = "delete FROM worker WHERE money> 1500"; s.exe cuteUpdate (SQL );} // display each record in the order of 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.exe cuteQuery (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" + "\ 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 Database Programming

String str = "81 Ubiquitin E3 ligase (SKP1A, FBXW8, CUL7, RBX1) 9820,26259, 9978,6500 ";
Assume that the above is a string input.
String id = str. substring (0, str. indexOf (""));
String name = str. substring (str. indexOf ("") + 1, str. lastIndexOf (""));
String subunits = str. substring (str. lastIndexOf ("") + 1 );
String [] subunit = subunits. split (",");
For (int I = 0; I <subunit. length; I ++ ){

Update (id, name, subunit [I]);

}
Write a corresponding update method using JDBC to complete the requirement!


Will XP make full use of hardware than 98, so that the game runs smoothly?

As a system that has served for more than ten years, it has ushered in its own home. Now, netizens around the world can't help but respect this tenacious system that exists in microsoft for more than 10 years. Only by constantly exploring, trying, and innovating can we make the system operation more humane. This is not comparable to XP 7 and 8.1.

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.