通过Java的JDBC访问MySQL数据库实现过程:
1. 安装mysql-5.0.24-win32:执行Setup.exe
在安装的过程中有个语言选项,要设置成gbk(简体中文)默认的是拉丁文。
否者存储的字母和汉子都不能识别而显示成问好“?”。
如果在安装的时候没有进行手动选择,可以执行
开始 --> MySQL --> MySQL Server Instance Configuration Wizard
在Please select the default character set中选择第三项
Manual Selected Default Character Set/Collation并在其下选择默认语言为gbk(简体中文)
2.安装数据库管理界面:执行MyManagerPro.exe
在安装完后出现的管理界面(SQL Manager 2005 for MySQL UNREGISTERED)中,
可以创建数据库。
3.将MySQL添加到工程的方法:
将mysql-connector-java-5.1.10-bin.jar复制到src下的某路径,Eclipse工程管理窗口中在mysql-connector-java-5.1.10-bin.jar
上右键 --> Build Path --> added to build path 自动产生默认的libs。
4.创建表(注意数据类型varchar(20))及记录的增加、查找、更新、删除
程序代码如下:
package testJDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestSQL {
public static void main(String[] args) {
TestSQL testSQL = new TestSQL();
// testSQL.testSelect();
// testSQL.testInsert();
// testSQL.testDelete();
// testSQL.testUpdate();
// testSQL.testCreatTable();
}
//创建表
private void testCreatTable() {
Connection cn = null;
PreparedStatement ps = null;
try {
cn = this.getConnection();
ps = cn.prepareStatement("create table customer1(n_id int,s_name
varchar(20),s_note varchar(20))");
ps.execute();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ps.close();
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//更新表中记录
private void testUpdate() {
Connection cn = null;
PreparedStatement ps = null;
try {
cn = this.getConnection();
String sName = "老胡";
ps = cn.prepareStatement("update customer1 set s_name=? where
n_id=?");
ps.setString(1, sName);
ps.setInt(2, 5);
ps.execute();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ps.close();
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//删除记录
private void testDelete() {
Connection cn = null;
PreparedStatement ps = null;
try {
cn = this.getConnection();
ps = cn.prepareStatement("delete from customer1");
ps.execute();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ps.close();
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private Connection getConnection(){
Connection cn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/test","root","baobeiwanan");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return cn;
}
//插入记录
private void testInsert() {
Connection cn = null;
PreparedStatement ps = null;
try{
cn = this.getConnection();
ps = cn.prepareStatement("insert into customer1 (s_name,s_note)
values('王','用')");
ps.execute();
}catch(Exception e){
e.printStackTrace();
} finally{
try {
ps.close();
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查看表中记录
public void testSelect(){
ResultSet rs = null;
Connection cn = null;
PreparedStatement ps = null;
try {
cn = this.getConnection();
ps = cn.prepareStatement("select * from customer1");
rs = ps.executeQuery();
while(rs.next()){
String name = rs.getString(2);
String note = rs.getString("s_note");
System.out.println("name:"+name+"/tnote:"+note);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
rs.close();
ps.close();
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}