package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestJDBC {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
// 建立与数据库的Connection连接
// 数据库所处于的ip:127.0.0.1 (本机)
// 数据库的端口号: 3306 (mysql专用端口号)
// 数据库名称 studinfo
// 编码方式 UTF-8
// 账号 root
// 密码 admin
Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/studinfo?characterEncoding=UTF-8",
"root", "admin");
System.out.println("连接成功,获取连接对象: " + c);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package jdbc;
import java.util.List;
import charactor.student;
public interface DAO{
//增加
public void add(student stud);
//修改
public void update(student stud);
//删除
public void delete(int id);
//获取
public Hero get(int id);
//查询
public List<student> list();
//分页查询
public List<student> list(int start, int count);
}
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
static String ip = "127.0.0.1";
static int port = 3306;
static String database = "student";
static String encoding = "UTF-8";
static String loginName = "root";
static String password = "admin";
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
}
// update 实现代码片段
public update(Student stu) {
boolean result = false;
if (stu == null) {
return result;
}
try {
// check
if (queryBySno(stu.getSno()) == 0) {
return result;
}
// 实现update
String sql = "update student set id=?,name=?,class=?,sex=?";
String[] param = { stu.getId(), stu.getName(), stu.getClass(), stu.getSex() };
int rowCount = db.executeUpdate(sql, param);
if (rowCount == 1) {
result = true;
}
} catch (SQLException se) {
se.printStackTrace();
} finally {
destroy();
}
return result;
}
String sql = "delete from student where id=?”;
String[] param = { stu.getId()};
String sql = "insert into student(id,name,class,sex) values(?,?,?,?)";
String[] param = { stu.getId(), stu.getName(), stu.getClass(), stu.getSex()};
private int queryById(String id) throws SQLException {
int result = 0;
if ("".equals(id) || id == null) {
return result;
}
String checkSql = "select * from student where id=?";
String[] checkParam = { id };
rs = db.executeQuery(checkSql, checkParam);
if (rs.next()) {
result = 1;
}
return result;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有