| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1588 人关注过本帖
标题:jdbc插入记录
只看楼主 加入收藏
bizika526
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-10-20
收藏
 问题点数:0 回复次数:1 
jdbc插入记录
package

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

/**
 *
 *用单子设计模式连接数据库
 *
 */
public class ConnectionDB {
    private static Connection conn = null;

    private ConnectionDB() {

    }
    
    public static Connection ConnectionDB(){
        if(conn==null){
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                String url="jdbc:oracle:thin:@localhost:1521:system";
                String user="scott";
                String password="tiger";
                conn=DriverManager.getConnection(url, user, password);
                System.out.println("数据库连接成功...........");
            } catch (ClassNotFoundException e) {
                System.out.println("数据库驱动异常:"+e.getMessage());
                e.printStackTrace();
            } catch (SQLException e) {
                System.out.println("数据库连接异常"+e.getMessage());
                e.printStackTrace();
            }
        }
        return conn;
    }
    
    /**
     * 关闭数据库连接
     */
    
    public static void closeDB(PreparedStatement ps,
            ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                System.out.println("关闭数据库异常:"+e.getMessage());
                e.printStackTrace();
            }
        }if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                System.out.println("关闭数据库异常2"+e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
---------------------------------
package

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJdbc1 {

    /**
     * JDBC连接数据库
     */

    private Connection conn = ConnectionDB.ConnectionDB();

    private PreparedStatement ps = null;

    private ResultSet rs = null;
    
    private int rs1=0;

    public static void main(String[] args) {
          TestJdbc1 test=new TestJdbc1();
          //Scanner sc=new Scanner(System.in);
          //System.out.print("进行模糊查询,请输入他的姓名中的任意字");
          //test.findInfo();
          //String S=sc.nextLine();
          //test.likeFindInfo(S);
          test.insertInfo(70,"国防部","北京");
    }
    /**
     *
     * @param id
     * 插入记录
     * @param loc
     */
    private void insertInfo(int id,String name,String loc) {
        StringBuffer sql3=new StringBuffer(
                "insert into  dept ");
        if(id!=0&&name!=null&&loc!=null){
            sql3.append("  values("+id+",'"+name+"','"+loc+"')");
        }
        try {
            ps=conn.prepareStatement(sql3.toString());
            System.out.println(sql3.toString());
            //System.out.println("部门号" + "\t" + "姓名                                 "+"籍贯");
           
            rs1=ps.executeUpdate();
            //System.out.println(rs1);
            while(rs.next()){
                System.out.print(rs.getInt("deptno")+"\t");
                System.out.print(rs.getString("dname")+"\t");
                System.out.print(rs.getString("loc")+"\t");
                System.out.println();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     *
     * 模糊查询
     */
    private void likeFindInfo(String name) {
        
        /*String sql2="select deptno,dname,loc from " +
                "dept where 1==1";*/
        StringBuffer sql2=new StringBuffer("select * from dept where 1=1");
        
        /*if(id!=0){
            sql2.append(" and deptno like '%");//注意里面的单引号
            sql2.append(id);
            sql2.append("%'");
        }*/
        if(name!=null){
            sql2.append("  and dname like '%");
            sql2.append(name);
            sql2.append("%'");
            }
        try {
            ps=conn.prepareStatement(sql2.toString());
            System.out.println(sql2.toString());
            System.out.println("部门号" + "\t" + "姓名                                 "+"籍贯");
            rs=ps.executeQuery();
            while(rs.next()){
                System.out.print(rs.getInt("deptno")+"\t");
                System.out.print(rs.getString("dname")+"\t");
                System.out.print(rs.getString("loc")+"\t");
                System.out.println();
            }
        } catch (SQLException e) {
            System.out.println("查询用户信息报错:"+e.getMessage());
            e.printStackTrace();
        }finally{
            ConnectionDB.closeDB(ps, rs);
        }
        
    }

    public void findInfo() {
        String sql = "select deptno,dname,loc from dept";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            System.out.println("部门号" + "\t" + "姓名                                 "+"籍贯");
            while (rs.next()) {
                System.out.print(rs.getInt(1) + "\t");
                System.out.print(rs.getString(2) + "\t");
                System.out.print(rs.getString(3) + "\t");
                System.out.println();

            }
        } catch (SQLException e) {
            System.out.println("查询用户信息报错" + e.getMessage());
            e.printStackTrace();
        }finally{
            ConnectionDB.closeDB(ps, rs);
        }
    }
    
    

}
那个插入业务不能实现啊??
数据库连接成功...........
insert into  dept   values(70,'国防部','北京')
Exception in thread "main" java.lang.NullPointerException
    at (TestJdbc1.java:51)
    at (TestJdbc1.java:30)
该怎么解决啊?、谢谢,请帮忙改一下并说明原因
搜索更多相关主题的帖子: jdbc 记录 
2008-11-19 02:20
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
粗略看了下,insertInfo中的rs貌似是空
while(rs.next()){
                System.out.print(rs.getInt("deptno")+"\t");
                System.out.print(rs.getString("dname")+"\t");
                System.out.print(rs.getString("loc")+"\t");
                System.out.println();
            }
2008-11-19 10:22
快速回复:jdbc插入记录
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022363 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved