:
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 Soulignement produit par Actipro CodeHighLighter (Freeware) http://www.codehighlighter.com/--> Importer java.sql. *;
classe publique mysql {
Url de chaîne statique publique = "JDBC: MySQL: // LocalHost: 3306 / Test"; // CaractorEncoding = GBK
public static String username = "root";
public statique String mot de passe = "root";
Connexion statique publique Con;
déclaration statique publique STMT;
Resset statique publique Rs;
public static void main (String [] args) lève Sqlexception {
connecter();
opération();
stmt.close ();
con.close ();
}
Test de void statique public () {
String SQL_Select = "SELECT * FROM TRAPENAME WHERE ID = 1";
String sql_insert = "Insérer dans TableName (Col1, Col2 ..) VALEUR ('1', '2' ...)";
String sql_update = "Update TableName set colname = 'update' où id = 1";
// insert (sql_insert);
// select (sql_select);
// update (sql_update);
}
public static void connect () {
// 定位驱动
essayer {
Class.forname ("com.mysql.jdbc.driver");
System.out.println ("加载驱动成功!");
} catch (classNotFoundException e) {
System.out.println ("加载驱动失败!");
e.printStackTrace ();
}
// 建立连接
essayer {
con = driverManager.getConnection (URL, nom d'utilisateur, mot de passe);
stmt = con.creataStation ();
System.out.println ("数据库连接成功!");
} catch (sqlexception e) {
System.out.println ("数据库连接失败!");
}
}
public static void select (String sql) {
essayer {
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 ("数据查询失败!");
}
}
Insert public statique void (String SQL) {
essayer {
stmt.clearbatch ();
STMT.AddBatch (SQL);
stmt.ExECuteBatch ();
System.out.println ("数据插入成功!");
} catch (exception e) {
System.out.println ("数据插入失败!");
}
}
Public static void Update (String SQL) {
essayer {
STMT.ExecuteUpdate (SQL);
System.out.println ("数据更新成功!");
} catch (exception e) {
System.out.println ("数据更新失败!");
}
}
}