java 在数据库中添加新信息

package lianxi1;

import java.sql.*;

import java.util.*;

public class lianxi1 {
    
    
    public static void main(String[] args) {
        //定义Connection 
        Connection conn = null;
        try {
            //链接数据库
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String strUrl ="jdbc:Oracle:thin:@localhost:1521:ORCL";
            conn = DriverManager.getConnection(strUrl, "test1", "duibuqi19950220");
            System.out.println("链接数据库成功!");
            //连接成功之后定义插入的信息
            System.out.println("请输入考生的详细信息!");
            System.out.println("Type: ");
            Scanner a = new Scanner (System.in);
            int aa = a.nextInt();
            System.out.println("IDCard:");
            Scanner b = new Scanner (System.in);
            String bb = b.next();
            System.out.println("ExamCard:");
            Scanner c = new Scanner (System.in);
            String cc = c.next();
            System.out.println("StudentName:");
            Scanner d = new Scanner (System.in);
            String dd = d.next();
            System.out.println("Location:");
            Scanner e = new Scanner (System.in);
            String ee = e.next();
            System.out.println("Grade:");
            Scanner f = new Scanner (System.in);
            int ff = f.nextInt();
            //定义一个创建方法的对象
            Statement st = conn.createStatement();
            //用insert into执行数据库的代码不需要加列名
            st.execute("insert into examstudent values (flowid.nextval,"+aa+",'"+bb+"','"+cc+"','"+dd+"','"+ee+"',"+ff+")");
            ResultSet rs = st.executeQuery("select * from EXAMSTUDENT");
            //遍历数据库的表
            while(rs.next())
            {
                int FlowID = rs.getInt(1);
                int Type = rs.getInt(2);
             String IDCard = rs.getString(3);
             String ExamCard = rs.getString(4);
             String StudentName = rs.getString(5);
             String Location = rs.getString(6);
               int Grade = rs.getInt(7);
             System.out.println("FlowID="+FlowID+"Type="+Type+"IDCard="+IDCard+"ExamCard="+ExamCard+"StudentName="+StudentName+"Location="+Location+"Grade="+Grade);
             
            }rs.close();
            //关闭连接,释放资源
            conn.close();
            st.close();
            System.out.println("录用信息成功!");
            
            } 
        
        catch (Exception e) 
        {
            e.printStackTrace();
            
        }
    
    }

}

 

posted @ 2016-06-24 10:17  薛岩  阅读(2218)  评论(0编辑  收藏  举报