JDBC的学习

来源:互联网 发布:淘宝咖啡猫游戏代购 编辑:程序博客网 时间:2024/06/03 21:50
package com.company;import java.sql.*;public class Main {    //public static void Insertdept(int deptno, String dname, String loc)throws ClassNotFoundException, SQLException    public static void Updatedept(String loc,int deptno)throws ClassNotFoundException, SQLException    {//        Class.forName("oracle.jdbc.driver.OracleDriver");//        String strUrl="jdbc:oracle:thin:@localhost:1521:orcl";//        Connection con=DriverManager.getConnection(strUrl,"scott","lwz315");//        Statement st= con.createStatement();//        String strSql="insert into dept values("+deptno+",'"+dname+"','"+loc+"')";//        st.executeUpdate(strSql);//        st.close();//        con.close();        Class.forName("oracle.jdbc.driver.OracleDriver");        String strUrl="jdbc:oracle:thin:@localhost:1521:orcl";        Connection con=DriverManager.getConnection(strUrl,"scott","lwz315");        Statement st= con.createStatement();        String strSql="UPDATE dept set loc='"+loc+"' where deptno="+deptno;        st.executeUpdate(strSql);        st.close();        con.close();    }    public static void main(String[] args) throws SQLException, ClassNotFoundException    {//// write your code here//    //1.加载jdbc驱动//        Class.forName("com.mysql.jdbc.Driver");//    //2.定义连接url//        String strUrl ="jdbc:mysql://localhost:3306/scott";//    //3.获取数据库连接//        Connection con= DriverManager.getConnection(strUrl,"root","root");//    //4.获得statement对象(用来执行sql语句,并返回结果)//        Statement st=con.createStatement();//    //5.执行查询或更新//        String strSql="select * from emp";//    //6.处理结果(遍历获取出来的所有数据)//        ResultSet rs = st.executeQuery(strSql);////        while (rs.next())//        {//            int nEmpNo=rs.getInt("empno");//            String strName=rs.getString("ename");//            String strJob=rs.getString("job");//            Date d=rs.getDate("hiredate");//            System.out.println(nEmpNo+" "+strName+" "+d+" "+strJob);//        }//    //7.关闭连接(释放资源)//        rs.close();//        st.close();//        con.close();      //  --------------------//            Class.forName("oracle.jdbc.driver.OracleDriver");//            String strUrl="jdbc:oracle:thin:@localhost:1521:orcl";//            Connection con=DriverManager.getConnection(strUrl,"scott","lwz315");//            Statement st= con.createStatement();//            String strSql="select * from emp";//            ResultSet rs=st.executeQuery(strSql);//            while (rs.next())//            {//                int nEmpNo=rs.getInt("empno");//                String strName=rs.getString("ename");//                String strJob=rs.getString("job");//                System.out.println(nEmpNo+" "+strName+" "+strJob);//            }//            rs.close();//            st.close();//            con.close();        //Insertdept(90,"技术部","沈阳");        Updatedept("铁岭",90);    }}

0 0
原创粉丝点击