1.现在MySQL中创建一个表
2.首先创建一个类
//导入的包
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBTil { }
3.然后实例化各个对象
//username和password是连接数据库的用户名和密码
private static String username = "123";
private static String password = "12345";
//连接MySQL的地址
private static String url = "jdbc:MySql://localhost:3306/mydb";
//连接MySQL的驱动
private static String dirver = "com.mysql.jdbc.Driver";
//创建建立连接时的对象
static Connection conn = null;
//创建PreparedStatement实例,用以执行sql语句
static PreparedStatement preparedStatement = null;
//结果集对象
static ResultSet rs = null;
4.接着写查询表方法
//参看表
public static void comtrue(){
String sql = "select * from user";
try {
Class.forName(dirver);
conn = DriverManager.getConnection(url,username,password);
preparedStatement = conn.prepareStatement(sql);
rs = preparedStatement.executeQuery();
while(rs.next()){
System.out.println(rs.getString(4)+" "+rs.getString(3)+" "+rs.getString(2)+" "+rs.getString(1));
}
System.out.println();
close(rs, conn, preparedStatement); } catch (Exception e) {
e.printStackTrace();
}
}
5.看控制台下输出的结果:
6.接着写添加的方法:
//添加
public static void add(){
String sql = "insert into user(id,username,age,sex) value(?,?,?,?)";
try {
int result = 0;
Class.forName(dirver);
conn = DriverManager.getConnection(url,username,password);
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, 5);
preparedStatement.setString(2, "小明");
preparedStatement.setInt(3, 10);
preparedStatement.setString(4, "男");
result = preparedStatement.executeUpdate();
System.out.println("插入成功");
close(rs, conn, preparedStatement);
} catch (Exception e) {
e.printStackTrace();
}
}
7.在原来表的基础上添加id为5的一行数据,看结果:
看添加成功了!
8.后面删除再实现下:
//删除
public static void delete(){
String sql = "delete from user where id = 4";
try {
Class.forName(dirver);
conn = DriverManager.getConnection(url,username,password);
preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
System.out.println("删除成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
看结果:
删除成功!
男 20 张三 1
女 30 李四 2
男 40 Mike 3
男 10 小明 5
9.修改实现方法:
//修改
public static void alter(){
String sql = "update user set username='小红'where id = 4"; try {
Class.forName(dirver);
conn = DriverManager.getConnection(url,username,password);
preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
看结果:
男 20 张三 1
女 30 李四 2
男 40 Mike 3
男 10 小红 5
改成小红了!
10.如果想查看某行数据:
//查询,以查询第一行数据为例
//查询
public static void inquire(){
String sql = "select * from user limit 0 , 1";
try {
Class.forName(dirver);
conn = DriverManager.getConnection(url,username,password);
preparedStatement = conn.prepareStatement(sql);
rs = preparedStatement.executeQuery();
while (rs.next()) {
System.out.println(
rs.getString(1)
+rs.getString(2)
+rs.getString(3)
+rs.getString(4));
}
} catch (Exception e) {
// TODO: handle exception
}
}
执行结果:
1张三20男
11.测试方法:
//测试
public static void main(String[] args) {
/*delete();*/
/* add();
alter();*/
/*inquire();*/
/*comtrue();*/
} }
12.小结:
通过控制台运行测试来实现简单的jdbc连接,增删改查操作,作为一个初学者,我觉得这样的测试很有必要,以后的jdbc连接操作都将以此为基础