如果在本地搭一个服务器和mysql数据库环境,如果使用java来访问_MySQL
我们可以使用speedamp来搭一个服务器环境,可以在http://download.csdn.net/detail/baidu_nod/7630265下载
解压后无需安装直接可以使用,点击SpeedAMP.exe,然后可以通过http://localhost/phpmyadmin/index.php来访问本地数据库
java可以通过jdbc来访问数据库,比较重要的是下载一个mysql-connector-java-5.1.22-bin.jar文件,然后在java工程中引用,
在代码里可以这样来访问:
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" + ");"; public static void main(String[] args) { java.sql.Connection conn = null; Statement st = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver");//加载jar包 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8","root","");//取得数据库连接 //这个是创建一个表 st = conn.createStatement(); st.execute(CREATE_TABLE_STMT); //这是插入一条语句// st = conn.createStatement();// String sql = "insert into spider(_id,name,class) values ('1','xiaoming','A')";// st.executeUpdate(sql); //这是采用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.executeUpdate(); // //这是一条更新语句// st = conn.createStatement();// String sql = "update spider set class = 'C' where _id = '1'";// st.executeUpdate(sql); // //这是一条删除语句// st = conn.createStatement();// String sql = "delete from spider where _id = '1'";// st.executeUpdate(sql); //这是查询语句 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(); } } }}
我们可以通过这种方式来锻炼写sql语句

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)
