If you are using a server and MySQL database environment locally, if you use Java to access the database

Source: Internet
Author: User

We can use Speedamp to build a server environment that can be downloaded in http://download.csdn.net/detail/baidu_nod/7630265


No installation can be used directly after decompression, click SpeedAMP.exe, then you can access the local database via 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 visit:

Import java.sql.*;p Ublic 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 + "(" + _i D + "INTEGER PRIMARY KEY," + NAME + "text," + class_name+ "text" + ");"; public static void Main (string[] args) {Java.sql.Connection conn = null; Statement st = null; ResultSet rs = null;try {class.forname ("com.mysql.jdbc.Driver");//load JAR Package conn = drivermanager.getconnection ("JDBC: Mysql://localhost:3306/test?useunicode=true&characterencoding=utf8 "," Root "," ");//Get database connection//This is to create a table St = Conn.createstatement (); St.execute (create_table_stmt);//This is inserting a statement//st = Conn.createstatement ();//string sql = "Insert Into Spider (_id,name,class) VALUES (' 1 ', ' xiaoming ', ' A '),//st.executeupdate (SQL);//This is an INSERT statement with PreparedStatement implementation 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.executeupdate ();////This is an UPDATE statement//st = Conn.createstatement () ;//string sql = "Update spider set class = ' C ' where _id = ' 1 '";//st.executeupdate (sql);////This is a DELETE statement//st = Conn.createstat Ement ();//string sql = "Delete from spider where _id = ' 1 '";//st.executeupdate (sql);//This is the query statement st = Conn.createstatement (); String sql = "SELECT * from Spider", rs = st.executequery (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 this way to write SQL statements

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.