java实现一个学生信息表相应要求

设有一个学生信息表,包括学号、姓名、出生日期、性别和地址,要求ODBC指定的数据库完成以下功能:

1. 建立student表格;

2. 给student表写入若干数据;

3. 显示student表中所有学生。

创建:

package chapter;
import java.sql.*;
public class CreateStudentTable {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String username;
		String password;
		username="";
		password="";
		String url="jbdc:odbc:studentdata";
		String dropString="drop table student";
		String createString="create table student"+"(xuehao INT,"+"name VARCHAR(8),"+"born datetime,"+"sex char(2),"+"address VARCHAR(32))";
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(java.lang.ClassNotFoundException e){
			System.err.print("ClassNotFoundException:");
			System.err.println(e.getMessage());
		}
		try{
			Connection con;
			Statement stmt;
			con=DriverManager.getConnection(url,username,password);
			stmt=con.createStatement();
			try{
				stmt.executeUpdate(dropString);
				System.out.println("学生信息表成功删除!");
			}
			catch(Exception ex){}
			stmt.executeUpdate(createString);
			System.out.println("学生信息表创建成功!");
			stmt.close();
			con.close();
		}
		catch(SQLException ex){
			System.err.println("==>SQLException:");
		while(ex!=null){
			System.out.println("Message:"+ex.getMessage());
			System.out.println("SQLState:"+ex.getSQLState());
			System.out.println("ErrorCode:"+ex.getErrorCode());
			ex=ex.getNextException();
		}
		}

	}
}


插入:

package chapter;
import java.sql.*;
public class InsertStudents {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String username;
		String password;
		username="";
		password=null;
		String url="jbdc:odbc:studentdata";
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}catch(java.lang.ClassNotFoundException e){
			System.err.print("ClassNotFoundException:");
			System.err.println(e.getMessage());
	}
		try{
			Connection con;
			Statement stmt;
			con=DriverManager.getConnection(url,username,password);
			stmt=con.createStatement();
			stmt.executeUpdate("INSERT INTO student"+"VALUES (200011101,'张三','1992/10/01'),'男','江西')");
			stmt.executeUpdate("INSERT INTO student"+"VALUES (200011112,'李四','1991/03/12'),'女','北京')");
			System.out.println("新插入两条记录到学生表中");
			stmt.close();
			con.close();
		}
		catch(SQLException ex){System.err.println("==>SQLException:");
		while(ex!=null){
			System.out.println("Message:"+ex.getMessage());
			System.out.println("SQLState:"+ex.getSQLState());
			System.out.println("ErrorCode:"+ex.getErrorCode());
			ex=ex.getNextException();
		}
	}
	}	
}

显示:

package chapter;
import java.sql.*;
public class QueryStudents {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String username;
		String password;
		username="";
		password=null;
		String url="jbdc:odbc:studentdata";
		String queryString="SELECT name,born,address FROM student";
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	}catch(java.lang.ClassNotFoundException e){
		System.err.print("ClassNotFoundException:");
		System.err.println(e.getMessage());
		
}
		try{
			Connection con;
			Statement stmt;
			con=DriverManager.getConnection(url,username,password);
			stmt=con.createStatement();
			ResultSet rs=stmt.executeQuery(queryString);
			while(rs.next()){
				String s=rs.getString("name");
				Date d=rs.getDate("born");
				String p=rs.getString("address");
				System.out.println(s+" "+d+" "+p);
			}
			stmt.close();
			con.close();
		}
		catch(SQLException ex){
			System.err.println("==>SQLException:");
		}
	}
}

运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
欢迎您关注我的微信公众号:学习微站(studysth)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习微站公众平台

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值