码迷,mamicode.com
首页 > 数据库 > 详细

java 在数据库中添加新信息

时间:2016-06-24 10:51:22      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

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();
            
        }
    
    }

}

 

java 在数据库中添加新信息

标签:

原文地址:http://www.cnblogs.com/zzyu/p/5613288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!