网络编程 
首页 > 网络编程 > 浏览文章

java操作mysql入门代码实例(含插入、更新和查询)

(编辑:jimmy 日期: 2024/9/21 浏览:3 次 )

复制代码 代码如下:
import java.sql.*;

public class mysql {
    public static String url = "jdbc:mysql://localhost:3306/test";//characterEncoding=GBK
    public static String username = "root";
    public static String password = "root";
    public static Connection con;
    public static Statement stmt;
    public static ResultSet rs;

    public static void main(String[] args) throws SQLException {
        connect();
        operation();
        stmt.close();
        con.close();
    }
    public static void test() {
        String sql_select = "select * from tablename where id=1";
        String sql_insert = "insert into tablename (col1,col2..) values('1','2'...)";
        String sql_update = "update tablename set colname='update' where id=1";
        //insert(sql_insert);
        //select(sql_select);
        //update(sql_update);
    }
    public static void connect() {
        // 定位驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("加载驱动成功!");
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动失败!");
            e.printStackTrace();
        }
        // 建立连接
        try {
            con = DriverManager.getConnection(url, username, password);
            stmt = con.createStatement();
            System.out.println("数据库连接成功!");
        } catch(SQLException e) {
            System.out.println("数据库连接失败!");
        }
    }
    public static void select(String sql) {
        try {
            rs = stmt.executeQuery(sql);
            ResultSetMetaData meta_data = rs.getMetaData();//列名
            for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {
                System.out.print(meta_data.getColumnLabel(i_col) + "   ");
            }
            System.out.println();
            while (rs.next()) {
                for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {
                    System.out.print(rs.getString(i_col) + "  ");
                }
                System.out.println();
            }
            rs.close();
        }catch (Exception e) {
            System.out.println("数据查询失败!");
        }
    }
    public static void insert(String sql) {
        try {
            stmt.clearBatch();
            stmt.addBatch(sql);
            stmt.executeBatch();
            System.out.println("数据插入成功!");
        }catch (Exception e) {
            System.out.println("数据插入失败!");
        }

    }
    public static void update(String sql) {
        try {
            stmt.executeUpdate(sql);
            System.out.println("数据更新成功!");
        }catch (Exception e) {
            System.out.println("数据更新失败!");
        }
    }
}

 

上一篇:struts2中一个表单中提交多个请求的例子(多个提交按钮)
下一篇:jsp跳转getRequestDispatcher()和sendRedirect()的区别
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap