zoukankan      html  css  js  c++  java
  • 一、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();


    }
    }

  • 相关阅读:
    从Android APP里面打开其他应用
    jQuery 中 serialize() 方法会受到asp.net 页面影响
    javascript 对象转换 json 的插件
    MVC 4将jQuery升级到1.9出现各种问题。。。
    用MVC做可拖拽的留言板,利用 Jquery模板 JsRender
    未能加载文件或程序集“Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
    处理MVC中默认的Json方法返回时间的问题
    jQuery 中使用$.post 无法获取 json
    MVC中用 BundleCollection 压缩CSS时图片路径问题
    MVC中返回Json的几种声明方式
  • 原文地址:https://www.cnblogs.com/frankchia/p/6216070.html
Copyright © 2011-2022 走看看