程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成mysql操作類分享 java銜接mysql

java完成mysql操作類分享 java銜接mysql

編輯:關於JAVA

java完成mysql操作類分享 java銜接mysql。本站提示廣大學習愛好者:(java完成mysql操作類分享 java銜接mysql)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成mysql操作類分享 java銜接mysql正文


相干軟件和驅動:
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("數據更新掉敗!");
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved