データベースインスタンスに接続します

データベースに接続するためのアイデアのステップの詳細な説明

package com.company;
import java.sql.*;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Student student = new Student();
        try {
    
    
            SqlOperation.main();
            ResultSet resultSet = ScoreSql.selectScoreSql();
            //输出所有行操作
            while(resultSet.next()){
    
    
            System.out.println(resultSet.getString("name"));
            System.out.println(resultSet.getFloat("score"));
            }
        }catch(Exception e) {
    
    
            e.printStackTrace();
            System.out.println("连接数据库失败!");
        }
    }
    SqlOperation.close();
}

データベースに接続し、操作オブジェクトを作成します

package com.company;

import java.sql.*;

public class SqlOperation {
    
    
    public static Connection connection = null;//定义连接数据库的对象(桥梁)
    public static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Studentinfo";
    public static Statement statement = null;//定义静态操作对象
    public static PreparedStatement preparedStatement = null;//定义动态操作对象
    public static void main() {
    
    
        try{
    
    
            //第一步加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("加载驱动成功!");
            //第二步建立连接
            connection = DriverManager.getConnection(url,"sa","shejiashuai");
            System.out.println("连接数据库成功!");
            //第三步建立操作对象
            statement = connection.createStatement();
        }catch (Exception e){
    
    
            e.printStackTrace();
            System.out.println("连接数据库失败!");
        }
    }
    public static void close(){
    
    //关闭对象
        try{
    
    
            statement.close();
            connection.close();
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
    }
}

package com.company;

import java.sql.ResultSet;
//创建操作成绩数据库的类
class ScoreSql {
    
    
    public static ResultSet selectScoreSql()throws Exception{
    
    
        return SqlOperation.statement.executeQuery("select * from Score");//调用SqlOperation.statement并使用其函数
    }
    public static void insertScoreSql(Score score)throws Exception{
    
    
        SqlOperation.statement.executeUpdate("insert into Score values ("+score.getName()+","+score.getScore()+")");
    }
    public static void createScoreSql()throws Exception{
    
    
        SqlOperation.statement.executeUpdate("create table Score(name char(10),score float)");
    }
    public static void deleteScoreSql(String name)throws Exception{
    
    
        SqlOperation.statement.executeUpdate("delete from Score where name = " +
                name);
    }
    public static void updateScoreSql(Score score)throws Exception{
    
    
        SqlOperation.statement.executeUpdate("update Score set score = " +
                score.getScore() + "where name = " +
                score.getName());
    }
    public static void statisticScoreSql(float score)throws Exception{
    
    //统计大于此分数的人
        SqlOperation.statement.executeUpdate("select * from Score where score > " +
                score);
    }
}
//下面是成绩类可省略不看
public class Score{
    
    
    String name;
    float score;

    public void setName(String name) {
    
    
        this.name = name;
    }

    public void setScore(float score) {
    
    
        this.score = score;
    }

    public String getName() {
    
    
        return name;
    }

    public float getScore() {
    
    
        return score;
    }
}



package com.company;

import java.sql.ResultSet;

class StudentSql{
    
    
    public static ResultSet selectStudentSql()throws Exception{
    
    
        return SqlOperation.statement.executeQuery("select * from Student where age > 18");
    }
}
//下面是学生类可省略不看
public class Student{
    
    
    private String name;
    private String sex;
    private String age;

    public String getName() {
    
    
        return name;
    }

    public String getSex() {
    
    
        return sex;
    }

    public String getAge() {
    
    
        return age;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public void setAge(String age) {
    
    
        this.age = age;
    }
}


おすすめ

転載: blog.csdn.net/qq_43907296/article/details/109321884