程序设计代码 贴着玩。。 三

简单数据库操作

package JAVA实验;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class MyDB{
	public MyDB(){
		try{
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
			System.out.println("Load the embedded driver");
			Connection conn = DriverManager.getConnection("jdbc:derby:myDB;create=true");
			System.out.println("create and connect to myDB");
			conn.setAutoCommit(false);
			
			Statement s = conn.createStatement();//System.out.println("hello");
			s.execute("create table employee(num varchar(40), name varchar(40), sex varchar(40), salary float)");
			System.out.println("Created employee Table");
			s.execute("insert into employee values('1001', '张强', '男', 675.20)");
			s.execute("insert into employee values('1004', '李香', '女', 842.00)");
			s.execute("insert into employee values('1007', '王大山', '男', 756.00)");
			s.execute("insert into employee values('1010', '赵玉花', '女', 690.00)");
			s.close();
			System.out.println("close result set and statement");
			conn.commit();
			conn.close();
			try{
				DriverManager.getConnection("jdbc:derby:;shutdown=true");
			} catch(SQLException se) {
				System.out.println("Database shutdown normally");
			} 
		}catch(Throwable a){
			System.out.println("创建数据库出现异常");
		}
	}
	void show(Connection conn){
		try{
			Statement s = conn.createStatement();
			ResultSet rs1 = s.executeQuery("select num, name, sex, salary from employee order by salary");
			System.out.println("所有员工记录表,按薪水高低排序");
			System.out.println("num\t\tname\t\tsex\t\tsalary");
			while(rs1.next()) {
				StringBuilder builder = new StringBuilder(rs1.getString(1));
				builder.append("\t\t");
				builder.append(rs1.getString(2));
				builder.append("\t\t");
				builder.append(rs1.getString(3));
				builder.append("\t\t");
				builder.append(rs1.getFloat(4));
				System.out.println(builder.toString());
			}
			rs1.close();
			s.close();
		} catch(Throwable e) {
			System.out.println("Error in show!");
		}
	}
}

public class MyDBDemo {
	public static void main(String[] args){
		MyDB myDB = new MyDB();
		try{
			//首先连接到所创建的数据库
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
			Connection connection = DriverManager.getConnection("jdbc:derby:myDB;");
			connection.setAutoCommit(false);
			
			// 接下来的代码用来显示所有男员工的记录
			Statement s = connection.createStatement();
			ResultSet rs = s.executeQuery("SELECT num, name, salary FROM employee where sex='男'");
			System.out.println("男性员工的记录");
			System.out.println("num\t\tname\t\tsalary");
			while(rs.next()){
				StringBuilder builder = new StringBuilder(rs.getString(1));
				builder.append("\t\t");
				builder.append(rs.getString(2));
				builder.append("\t\t");
				builder.append(rs.getFloat(3));//System.out.println("mygod");
				System.out.println(builder.toString());
			}
		    // 接下来从数据库中增加一条数据记录:2001,邢雪花,女,650
			System.out.println("增加记录之前:");
			myDB.show(connection);//显示修改之前所有员工的记录
			s.execute("insert into employee values('2001', '邢雪花', '女', 650.00)");
			System.out.println("增加记录之后:");
			myDB.show(connection);//修改之后所有员工的记录
			//接下来将num值为2001的记录中的salary改为900
			s.execute("update employee set salary=900.00 where num='2001'" );
			System.out.println("修改数据之后:");
			myDB.show(connection);
			s.execute("drop table employee");
			s.close();
			rs.close();
			connection.commit();
			connection.close();	
			try{
				DriverManager.getConnection("jdbc:derby:;shutdown=true");
			} catch(SQLException se) {
				System.out.println("Database shutdown normally");
			}
		} catch(Throwable a){
			System.out.println("Error in main!");
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值