添加:
实现代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc02 {
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "root";
//加载数据库访问驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//创建数据库链接
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("数据库已经连接成功了....");
String sql = "INSERT INTO hospitalstaff(hospitalId,postNum,section,staffName,staffSex,staffAge,birth,staffHome,fixedTelephone,moveTelephone) " +
"VALUES(110,7,403,'刘哲','男',49,'1971-11-25','新疆乌鲁木齐','070-154385','13565874258')\n";
Statement st = conn.createStatement();
int rows = st.executeUpdate(sql);
if (rows>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
st.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库链接失败....");
}
}
}
实现效果:
修改:
实现代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc02 {
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "root";
//加载数据库访问驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//创建数据库链接
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("数据库已经连接成功了....");
/*String sql = "INSERT INTO hospitalstaff(hospitalId,postNum,section,staffName,staffSex,staffAge,birth,staffHome,fixedTelephone,moveTelephone) " +
"VALUES(110,7,403,'刘哲','男',49,'1971-11-25','新疆乌鲁木齐','070-154385','13565874258')\n";*/
String sql="update hospitalstaff set postNum=8,section=403,staffName='李源',staffSex='女',staffAge=35,birth='1985-4-11',staffHome='宁夏银川',fixedTelephone='016-123548',moveTelephone='15326982463' where staffNum=8";
Statement st = conn.createStatement();
int rows = st.executeUpdate(sql);
if (rows>0){
//System.out.println("添加成功");
System.out.println("修改成功");
}else{
//System.out.println("添加失败");
System.out.println("修改失败");
}
st.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库链接失败....");
}
}
}
实现效果:
删除:
实现代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc02 {
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/myschool?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "root";
//加载数据库访问驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//创建数据库链接
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("数据库已经连接成功了....");
/*String sql = "INSERT INTO myschool(hospitalId,postNum,section,staffName,staffSex,staffAge,birth,staffHome,fixedTelephone,moveTelephone) " +
"VALUES(110,7,403,'刘哲','男',49,'1971-11-25','新疆乌鲁木齐','070-154385','13565874258')\n";*/
//String sql="update myschool set postNum=8,section=403,staffName='李源',staffSex='女',staffAge=35,birth='1985-4-11',staffHome='宁夏银川',fixedTelephone='016-123548',moveTelephone='15326982463' where staffNum=8";
String sql ="DELETE FROM student WHERE studentNo=7";
Statement st = conn.createStatement();
int rows = st.executeUpdate(sql);
if (rows>0){
//System.out.println("添加成功");
//System.out.println("修改成功");
System.out.println("删除成功");
}else{
//System.out.println("添加失败");
//System.out.println("修改失败");
System.out.println("删除失败");
}
st.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库链接失败....");
}
}
}
实现效果:
查询:
实现代码:
package jdbc01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestJdbc1{
public static void main(String[] args) throws Exception {
String url="jdbc:mysql://127.0.0.1:3306/myschool?useUnicode=true&characterEncoding=UTF-8";
String username="root";
String password="root";
//加载数据库的访问驱动
//Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver");
//创建数据库链接
Connection conn= DriverManager.getConnection(url,username,password);
System.out.println("数据库连接成功~~~");
//发送Sql语句
String sql="select * from student";
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(sql);
//处理结果集
while(rs.next()){
String studentName = rs.getString("studentName");
String loginPwd=rs.getString("loginPwd");
System.out.println(studentName);
System.out.println(loginPwd);
}
//关闭资源
rs.close();
st.close();
conn.close();
}
}
实现效果:
以上均为个人所写,如有错误,欢迎指正,谢谢~~