java實(shí)現(xiàn)mysql操作類分享 java連接mysql
相關(guān)軟件和驅(qū)動(dòng):
Mysql下載版本:4.1.11
http://dev.mysql.com/downloads/mysql/4.1.html
JDBC驅(qū)動(dòng)下載版本: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() {
// 定位驅(qū)動(dòng)
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加載驅(qū)動(dòng)成功!");
} catch (ClassNotFoundException e) {
System.out.println("加載驅(qū)動(dòng)失敗!");
e.printStackTrace();
}
// 建立連接
try {
con = DriverManager.getConnection(url, username, password);
stmt = con.createStatement();
System.out.println("數(shù)據(jù)庫連接成功!");
} catch(SQLException e) {
System.out.println("數(shù)據(jù)庫連接失敗!");
}
}
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("數(shù)據(jù)查詢失敗!");
}
}
public static void insert(String sql) {
try {
stmt.clearBatch();
stmt.addBatch(sql);
stmt.executeBatch();
System.out.println("數(shù)據(jù)插入成功!");
}catch (Exception e) {
System.out.println("數(shù)據(jù)插入失敗!");
}
}
public static void update(String sql) {
try {
stmt.executeUpdate(sql);
System.out.println("數(shù)據(jù)更新成功!");
}catch (Exception e) {
System.out.println("數(shù)據(jù)更新失敗!");
}
}
}
- java連接MySQl數(shù)據(jù)庫實(shí)例代碼
- java連接mysql數(shù)據(jù)庫亂碼的解決方法
- java jdbc連接mysql數(shù)據(jù)庫實(shí)現(xiàn)增刪改查操作
- Java連接MySql的詳細(xì)介紹
- java使用jdbc連接數(shù)據(jù)庫工具類和jdbc連接mysql數(shù)據(jù)示例
- java連接mysql數(shù)據(jù)庫詳細(xì)步驟解析
- Java連接MYSQL數(shù)據(jù)庫的實(shí)現(xiàn)步驟
- Java數(shù)據(jù)庫連接池的幾種配置方法(以MySQL數(shù)據(jù)庫為例)
- Java 通過JDBC連接Mysql數(shù)據(jù)庫
- Java中如何獲取mysql連接的3種方法總結(jié)
相關(guān)文章
SpringBoot ResponseBody返回值處理的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot ResponseBody返回值處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解
這篇文章主要介紹了mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Spring MVC url提交參數(shù)和獲取參數(shù)
本文重要講述通過url提交參數(shù)和獲取參數(shù)的具體操作與實(shí)現(xiàn)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04SpringMVC通過注解獲得參數(shù)的實(shí)例
下面小編就為大家?guī)硪黄猄pringMVC通過注解獲得參數(shù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08