If you build a local server and mysql database environment, if you use java to access _ MySQL

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.