If you build a local server and mysql database environment, if you use java to access the database, we can use speedamp to build a server environment, you can download it at http://download.csdn.net/detail/baidu_nod/7630265
After decompression, you can directly use the tool without installation. click speedamp.exe and access the local database through http: // localhost/phpmyadmin/index. php.
Java can access the database through jdbc, it is more important to download a mysql-connector-java-5.1.22-bin.jar file, and then reference in the java project,
In the code, you can access:
Import java. SQL. *; public class ConnTest {public static final String _ ID = "_ id"; public static final String TABLE_NAME = "spider"; public static final String NAME = "name "; public static final String CLASS_NAME = "class"; private static final String CREATE_TABLE_STMT = "create table if not exists" + TABLE_NAME + "(" + _ ID + "integer primary key, "+ NAME +" TEXT, "+ CLASS_NAME +" TEXT "+"); "; publi C static void main (String [] args) {java. SQL. connection conn = null; Statement st = null; ResultSet rs = null; try {Class. forName ("com. mysql. jdbc. driver "); // load the jar package conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test? UseUnicode = true & characterEncoding = utf8 "," root "," "); // Obtain the database connection // create a table st = conn.createStatement();st.exe cute (CREATE_TABLE_STMT ); // Insert a statement // st = conn. createStatement (); // String SQL = "insert into spider (_ id, name, class) values ('1', 'xiaoming', 'A ')"; // st.exe cuteUpdate (SQL); // insert a statement using preparedStatement // String SQL = "insert into spider (_ id, name, class) values (?,?,?) "; // PreparedStatement _ prepInsert = conn. prepareStatement (SQL); // _ prepInsert. setInt (1, 2); // _ prepInsert. setString (2, "xiaoli"); // _ prepInsert. setString (3, "B"); // _prepInsert.exe cuteUpdate (); // This is an update statement // st = conn. createStatement (); // String SQL = "update spider set class = 'c' where _ id = '1'"; // st.exe cuteUpdate (SQL ); //// this is a delete statement // st = conn. createStatement (); // String SQL = "delete from spider where _ Id = '1' "; // st.exe cuteUpdate (SQL); // This is the query statement st = conn. createStatement (); String SQL = "select * from spider"; rs = st.exe cuteQuery (SQL); while (rs. next () {int id = rs. getInt ("_ id"); String name = rs. getString ("name"); String class1 = rs. getString ("class"); System. out. println ("id =" + id + "name =" + name + "class1 =" + class1) ;}} catch (Exception e) {e. printStackTrace ();} if (rs! = Null) {try {rs. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (st! = Null) {try {st. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (conn! = Null) {try {conn. close () ;}catch (SQLException e) {e. printStackTrace ();}}}}
We can exercise the SQL statement writing in this way.