连接数据库实例

idea连接数据库步骤详解

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;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值