java连接Oracle(JDBC)

package text.coming;

import java.sql.*;

public class demo {
    public static void main(String[] args) {
        Connection ct=null;
        Statement statement=null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
             statement=ct.createStatement();
             int n=statement.executeUpdate("insert into student values(110,'厉害','鸡你太美')");
             System.out.println("成功插入数据!");
             System.out.println("影响的行数:"+n);
        } catch (Exception e) {
            e.printStackTrace();
        }
    //关闭资源
        finally
        {
            if(statement!=null)
            {
                try {
                    statement.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                statement=null;
            }
            if(ct!=null)
            try{
            ct.close();
            }
            catch(SQLException e)
            {
            e.printStackTrace();
            }
            ct=null;//使用垃圾回收机制    
        }
        
    }

}

 

posted @ 2024-04-09 22:46  困到很想醒  阅读(9)  评论(0编辑  收藏  举报