• 一、javase学习:数据库操作练习


    package JDBC_TEST;

    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;

    public class TestConn {
    /**
    * 步骤:
    * 1.加载JDBC驱动
    * 2.建立数据库连接
    * 3.创建Statement对象
    * 4.执行SQL语句
    * 5.处理返回结果
    * 6.关闭连接
    * @param args
    * @throws SQLException
    */
    public static void main(String[] args) throws SQLException {

    //实例化类对象:
    TestConn con=new TestConn();
    con.getConn();

    //4.执行SQL语句,获取用户列表信息
    // try {
    // con.queryUsersList();
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    //con.insert();

    }


    public Connection getConn(){
    /*1.加载驱动:放入第三方jar包(webiflibmysql-connector-java-5.1.25-bin),
    * 通过java.lang.class类加载该包
    */
    try {
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("连接成功");

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    //e.printStackTrace();
    System.out.println("找不到驱动程序");
    }
    /*2.创建连接
    * (1)通过jdbc提取url,
    * url格式:协议:子协议:数据库标识(jdbc://mysql://ip地址:端口/数据库)
    */
    String url="jdbc:mysql://localhost:3306/student";
    //(2)创建连接
    Connection conn=null;
    try {
    conn= (Connection) DriverManager.getConnection(url, "root", "");
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return conn;
    }
    /**
    * 3.创建Statement对象,查询数据库表users,显示列表信息
    * @throws SQLException
    */
    public void queryUsersList() throws SQLException{
    //连接数据库
    Connection con=getConn();
    ResultSet res=null;

    //创建Statement对象
    Statement sta=null;

    try {
    sta=(Statement) con.createStatement();
    //执行相关SQL语句
    String sql ="select * from users";
    res=sta.executeQuery(sql);
    while(res.next()) {//逐条显示
    //查询时可以根据列的序号,也可以通过列名查询
    System.out.print("姓名:"+res.getString(2)+" ");
    System.out.println("籍贯:"+res.getString("address"));
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    //6.关闭连接:注意先使用的后关闭


    res.close();
    sta.close();
    con.close();
    }

    }

    /**
    * 通过JDBC实现对数据库的新增记录操作
    * @throws SQLException
    */
    public void insert() throws SQLException{

    //1.加载驱动,建立对数据库连接
    Connection con= getConn();
    //2.创建一个Statement对象
    Statement st= (Statement) con.createStatement();
    String sql="insert into users (name,age) values('张三',20)";
    st.executeUpdate(sql);
    //3.关闭连接
    st.close();
    con.close();


    }
    }

  • 相关阅读:
    html优化
    HTML练习(网页计算器)
    hdu--4574 Bombs(dfs)
    Robots at Warehouse(搜索+vector的使用)
    poj 2111 Millenium Leapcow(记忆化搜索)
    Codeforces Round #408 (Div. 2) C. Bank Hacking(暴力啊!暴力)
    Gym
    Gym
    浙江省赛--D
    浙江省赛--C
  • 原文地址:https://www.cnblogs.com/frankchia/p/6216070.html
Copyright © 2020-2023  润新知