发表于: 2017-09-12 21:33:14

1 801


今天完成的事情:学习JDBC,写了简单的增删查改方法模板,

                             很多东西看不懂,进度很慢


明天计划的事情:学习如何把参数导入sql语句中,编写详细的DAOimpl实现方法


遇到的问题:在idea中运行程序时,出现错误,查不到怎么解决


setting中是这样的



收获

1.下载了mysql的driver包,导入项目中


2.写了一个JdbcUtils类,包含了:地址,用户名,密码,注册方法,连接方法,释放方法。

public final class JdbcUtils {
   private static String url = "jdbc:mysql://localhost:3306/jdbc";
   private static String user = "root";
   private static String password = "640913";

   private JdbcUtils(){
   }

   static {//注册驱动
       try {
           Class.forName("com.mysql.jdbc.Driver");
       } catch (ClassNotFoundException e) {
           throw new ExceptionInInitializerError(e);
       }
   }

   public static Connection getConnection() throws SQLException {//连接
       return DriverManager.getConnection(url,user,password);
   }

   public static void free(ResultSet rs, Statement st, Connection conn){//释放
       try{
          if (rs != null)
            rs.close();
       } catch (SQLException e){
          e.printStackTrace();
       }finally{
          try {
            if (st != null)
              st.close();
            } catch (SQLException e) {
              e.printStackTrace();
            }finally {
              if (conn != null)
                try {
                  conn.close();
                } catch (SQLException e) {
                  e.printStackTrace();
                }
            }
       }
   }
}

3.然后写了一个增删查改类

public class CRUD {
   static void read()throws SQLException{
       Connection conn = null;
       Statement st = null;
       ResultSet rs = null;

       try{
           conn = JdbcUtils.getConnection();
           st = conn.createStatement();

           String sql = "SELECT ID,name,qq,type,university,wish FROM student";
           rs = st.executeQuery(sql);
           while(rs.next()){
               System.out.println(rs.getObject("ID")
                    + "\t" + rs.getObject("name")
                    + "\t" + rs.getObject("qq")
                    + "\t" + rs.getObject("type")
                    + "\t" + rs.getObject("university")
                    + "\t" + rs.getObject("wish"));
           }
       }finally {
           JdbcUtils.free(rs,st,conn);
       }

   }


   static void add() throws SQLException{
       Connection conn = null;
       Statement st = null;
       ResultSet rs = null;

       try{
           conn = JdbcUtils.getConnection();
           st = conn.createStatement();

           String sql = "insert into student(name,qq,type,university,wish) values('xmj',111111,'chwl','jld','money')";
           int i = st.executeUpdate(sql);//返回值为int,表示几行数据被修改
           System.out.println("i = " + i);
       }finally {
           JdbcUtils.free(rs,st,conn);
       }

   }


   static void delete() throws SQLException{
       Connection conn = null;
       Statement st = null;
       ResultSet rs = null;

       try{
           conn = JdbcUtils.getConnection();
           st = conn.createStatement();

           String sql = "delete from student where id=3";
           int i = st.executeUpdate(sql);//返回值为int,表示几行数据被修改
           System.out.println("i = " + i);
       }finally {
           JdbcUtils.free(rs,st,conn);
       }

   }


   static void update() throws SQLException{
       Connection conn = null;
       Statement st = null;
       ResultSet rs = null;

       try{
           conn = JdbcUtils.getConnection();
           st = conn.createStatement();

           String sql = "update student set qq=qq+1";
           int i = st.executeUpdate(sql);//返回值为int,表示几行数据被修改
           System.out.println("i = " + i);
       }finally {
           JdbcUtils.free(rs,st,conn);
       }
   }
}




返回列表 返回列表
评论

    分享到