Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
public class Mysqljdbc {
@SuppressWarnings ("Rawtypes")
public static list<map> Findsql (String sql) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
First step: Load MySQL's JDBC driver
Class.forName ("Com.mysql.jdbc.Driver");
Gets the URL of the connection, can access the MySQL database user name, password; database name
String url = "Jdbc:mysql://192.168.1.10:3306/shaimei";
String user = "Cpdmembers";
String password = "Cpdmembers";
Step Two: Create an instance of the connection class with the MySQL database
conn = drivermanager.getconnection (URL, user, password);
Step Three: Create an instance of the statement object class with Conn stmt
stmt = Conn.createstatement ();
Fourth step: Execute the query, with the object of the ResultSet class, return the results of the query
String sql = "SELECT * from Rerule";
rs = stmt.executequery (SQL);
list<map> list = new arraylist<map> ();
while (Rs.next ()) {
map<string, object> map = new hashmap<string, object> ();
ResultSetMetaData RSMD = Rs.getmetadata ();
int count = Rsmd.getcolumncount ();
for (int i=1;i<count;i++) {
String key = Rsmd.getcolumnlabel (i);
Object value = Rs.getobject (i);
Map.put (key, value);
List.add (map);
}
SYSTEM.OUT.PRINTLN (list); Get data from the database
return list;
}
} catch (ClassNotFoundException e) {
A JDBC error was loaded and the driver used was not found
SYSTEM.OUT.PRINTLN ("Driver loading error");
}catch (SQLException ex) {
Display database connection errors or query errors
System.err.println ("SQLException:" +ex.getmessage ());
}finally {
try{
if (rs! = null) {
Rs.close ();
rs = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
IF (conn! = null) {
Conn.close ();
conn = null;
}
}catch (SQLException e) {
System.err.println ("SQLException:" +e.getmessage ());
}
}
return null;
}
public static void Main (string[] args) {
MySQL Access pack: http://download.csdn.net/detail/u010696272/8006839
System.out.println (Findsql ("SELECT * from Rerule"));
}
}
JDBC uses this one under MySQL.