数据库插入数据
USE school——java;
CREATE TABLE student (
id INT(11),
name VARCHAR(25),
tel INT(11),
sex VARCHAR(6)
);
DESC student;
插入十条数据
Java与数据库联动
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.cj.jdbc.Driver;
public class school_java {
Connection connection;
public static void main(String[] args) throws SQLException {
school_java schooljava=new school_java();
schooljava.getConnection();
schooljava.selectAll();
schooljava.add(11,"十一",1824567890,"男"); //添加三条数据
schooljava.add(12,"十二",1394567890,"女");
schooljava.add(13,"十三",1893154635,"男");
schooljava.select182(); //查找182
schooljava.xiugai(); //修改数据
schooljava.chanan(); //查找男生数据
schooljava.dsg(); //修改该数据姓名为大帅哥
schooljava.select139(); //查找139
schooljava.sc();
schooljava.chanv(); //查找女士数据
schooljava.selectAll(); //查找全部
}
public void add(int id, String name,int tel,String sex) throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("insert into student values(?,?,?,?);");
preparedStatement.setInt(1, id);
preparedStatement.setString(2, name);
preparedStatement.setInt(3,tel);
preparedStatement.setString(4,sex);
preparedStatement.executeUpdate();
}
public void getConnection()throws SQLException {
DriverManager.registerDriver(new Driver());//第一步:注册驱动
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_java","root","1234");//第二步:获取连接
}
public void selectAll() throws SQLException { //查询课程表所有数据
PreparedStatement preparedStatement=connection.prepareStatement("select*from student");//第三步:获取statement对象
ResultSet resultSet=preparedStatement.executeQuery();//第四步:执行SQL语句返回结果集
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));//第五步:遍历结果集
}resultSet.close();
preparedStatement.close();}
public void select182() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where tel like '182%'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}
}
public void xiugai() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("update student set tel=1312423530 where id=5");
preparedStatement.executeUpdate( );
}
public void chanan() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where sex='男'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}
}
public void dsg() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("update student set name='大帅哥' where id=10");
preparedStatement.executeUpdate( );
}
public void select139() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where tel like '139%'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}
}
public void sc() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("delete from student where id=3");
preparedStatement.executeUpdate( );
}
public void chanv() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where sex='女'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id")); //输出数据
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}}
}
最终数据库结果