JDBC案例解析

直接上代码,关于JDBC的解释什么的可以从隔壁看看。

TestJDBC.java

package ggg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class TestJDBC {
    private static String url = "jdbc:mysql://localhost:3306/mydb";
    private static String user = "root";
    private static String password = "123456";

    public static Connection getConnection() throws ClassNotFoundException, SQLException{
         Connection conn = null;
        //通过工具类获取连接对象
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection(url, user, password);
     return conn;   
    }


    /**
     * 增加
     */
    public void testInsert(){
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            //3.创建Statement对象
            stmt = conn.createStatement();

            //4.sql语句
            String sql = "insert into student values(7,'杨九',74,86,98)";

            //5.执行sql
            int count = stmt.executeUpdate(sql);

            System.out.println("影响了"+count+"行");

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //关闭资源
            if(stmt!=null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            //JdbcUtil.close(conn, stmt);
        }
    }

     // 删除
    public void testDelete(){
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            //3.创建Statement对象
            stmt = conn.createStatement();

            //4.sql语句
            String sql = "delete from student where id=7";

            //5.执行sql
            int count = stmt.executeUpdate(sql);

            System.out.println("影响了"+count+"行");

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //关闭资源
            if(stmt!=null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            //JdbcUtil.close(conn, stmt);
        }
    }

    /**
     * 查询
     */

    public void testselect(){
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            //3.创建Statement对象
            stmt = conn.createStatement();

            //4.sql语句
            String sql = "select *from student";

            //5.执行sql
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                int math=rs.getInt("math");
                int English=rs.getInt("English");
                int chinese=rs.getInt("chinese");
                System.out.println(id+"  "+name+"  "+math+"  "+English+"  "+chinese+"  ");
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //关闭资源
            if(stmt!=null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            //JdbcUtil.close(conn, stmt);
        }
    }
    /**
     * 更新
    */
    public void testupdate(){
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            //3.创建Statement对象
            stmt = conn.createStatement();

            //4.sql语句
            String sql = "update student set name='马十' where id=2";

            //5.执行sql
            int count = stmt.executeUpdate(sql);

            System.out.println("影响了"+count+"行");

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //关闭资源
            if(stmt!=null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            //JdbcUtil.close(conn, stmt);
        }
    }
    }

sq l文件:


SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
--  Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL,
  `math` tinyint(4) default NULL,
  `English` tinyint(4) default NULL,
  `chinese` tinyint(4) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records 
-- ----------------------------
INSERT INTO `student` VALUES ('1','张三','68','88','90'), 
('2','李四','68','88','90'), ('3','王五','45','68','96'), 
('4','赵六','75','68','88'), ('5','钱七','78','68','80'), 
('6','吴八','77','88','99');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值