国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現mysql操作類分享 java連接mysql

2019-11-26 15:45:17
字體:
來源:轉載
供稿:網友

相關軟件和驅動:
Mysql下載版本:4.1.11
http://dev.mysql.com/downloads/mysql/4.1.html

JDBC驅動下載版本:3.1.8
http://dev.mysql.com/downloads/connector/j/3.1.html

復制代碼 代碼如下:

代碼

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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("數據更新失敗!");
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 称多县| 普洱| 太康县| 新宾| 沅江市| 三都| 黎川县| 顺昌县| 安多县| 玉树县| 江安县| 渝北区| 鄢陵县| 肇东市| 广饶县| 翼城县| 剑河县| 通辽市| 开阳县| 梨树县| 石嘴山市| 牟定县| 栾城县| 东山县| 青冈县| 石家庄市| 星子县| 兴义市| 古蔺县| 河池市| 沐川县| 桐城市| 景宁| 孝昌县| 五大连池市| 满城县| 桂阳县| 高台县| 开原市| 南丰县| 兰西县|