我们能够使用speedamp来搭一个server环境,能够在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文件,然后在javaproject中引用,
在代码里能够这样来訪问:
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语句