我们可以使用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语句