发表于: 2017-03-14 22:03:09

4 1480


今天完成的任务:

   主要编写了DAO,并完成调试,实现了数据库的链接,并进行增删改查的功能。因为调试了一天,没有来得及完成注释。明天继续。

package com.vae.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

public class JdbcTest 

{

private final static String URL="jdbc:mysql://localhost:3306/student";

public final static String USERNAME="root";

public final static String PASSWORD="wobushi369";

    

public final static String DRIVER="com.mysql.jdbc.Driver";

public static void main(String[] args)

{

insert();

insert1();

update();

delete();

}

public static void insert()

{

try  

{

Class.forName(DRIVER);

Connection connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);

String sql="insert into student(name,age) values('胡靖',25)";

Statement state=connection.createStatement();

state.executeUpdate(sql);

state.close();

connection.close();

System.out.println("insert success");

}

catch (ClassNotFoundException e)

{

e.printStackTrace();

}

catch (SQLException e)

{

e.printStackTrace();

}

}

    public static void insert1()

    {

    String name="韩申明";

    int    age=27;

        try

        {

        Class.forName(DRIVER);

        Connection connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);

        String sql="insert into student(name,age) values('"+name+"',"+age+")";

        Statement state=connection.createStatement();

        state.executeUpdate(sql);

        state.close();

        connection.close();

        System.out.println("success.");

        }

        catch (ClassNotFoundException e)

        {

        e.printStackTrace();

        }

        catch (SQLException e)

        {

        e.printStackTrace();

        }

     }

    

    

     public static void update()

     {

    try

    {

    Class.forName(DRIVER);

    Connection connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);

    String sql="update student set age=25 where id=12";

    Statement state=connection.createStatement();

    state.close();

    connection.close();

    System.out.println("update success");

    }

     

    catch (ClassNotFoundException e)

    {

    e.printStackTrace();

    }

    catch(SQLException e)

    {

    e.printStackTrace();

    }

      }

    

     

      public static void delete()

      {

     try

     {

     Class.forName(DRIVER);

     Connection connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);

     String sql="delete from student where id=12";

     Statement state=connection.createStatement();

     state.executeUpdate(sql);

     state.close();

     System.out.println("delete success");

     }

     

     catch (ClassNotFoundException e)

     {

     e.printStackTrace();

     }

     catch (SQLException e)

     {

     e.printStackTrace();

     }

       }

}

明天的计划:

   开始新的步骤,学习spring,junit,并部署远程服务器。完成步骤18——22.听从师兄的建议,对自己的DAO进行优化,实现工具类的转变(预计两到三天时间,编DAO代码花了四五天,改进应该快一点)。

遇到问题:

   bug不断,千奇百怪,有拼写错误,有语法错误。还有一个,重启后好了。开始体会到程序员的烦恼和幸福了:)

收获:

   第一次完成了真正有实用意义的代码。Enjoy every step forward!Finting!



返回列表 返回列表
评论

    分享到