IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

【Java】连接数据库SQLServer

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

工具: eclipse
    Microsoft SQL Server
    SQL Server连接驱动:mssql-jdbc-6.4.0.jre8.jar

SQL script代码

CREATE DATABASE test
USE test
go

CREATE TABLE a 
(
    a1 nchar(3),            --String  NString
    a2 nvarchar(20),        --String  NString
    a3 int,             --int     int
    a4 float,           --double  double
    PRIMARY KEY (a1)
);

SELECT *
FROM a 
ORDER BY 1;

INSERT INTO a (a1, a2, a3, a4)
VALUES ();
  • 步骤:
    1. 将SQL打开,输入脚本script,运行
    2. 打开eclipse,新建JAVA项目
    3. 鼠标指向自己新建的项目,右击,建立文件夹
    4. 把mssql复制进文件夹内
    5. 右击mssql,点击build path -〉 add to build path
    6. 创建包com.jdbc,把两个JAVA文件复制进包中(JAVA database connectivity)
    7. 右击InsertSample.java,选properties
    8. Text file encoding 选other UTF-8采用Unicode字符串

Java 连接

所需的jar包下载

百度网盘 提取码: dm6m

使用方法:
  1. 在工程目录中创建lib文件夹,将下载好的JDBC(jar包)放到该文件夹下。
  2. 右键工程名,选择 Buiding Path | Configure Buiding Path ,在java build path中的Libraries分页中选择Add JARs…,选择刚才添加的JDBC(jar包)。

ConnectionProperty代码

package com.jdbc;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

public class ConnectionProperty 
{
    //Class.forName(...);   //这句话过时了 完全不需要
    //DriverManager.getConnection(...) //过时了 JAVA1.0中用
    public static SQLServerDataSource getDateSource() 
    {
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setServerName("10.177.7.47"); //本机IPv4号
        ds.setPortNumber(1433);     //端口号
        ds.setUser("sa");       //用户名
        ds.setPassword("123");  //密码
        ds.setDatabaseName("test");     //数据库名称
        ds.setSendTimeAsDatetime(false);
        return ds;
    }
}

InsertSample代码

package com.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import javax.sql.DataSource;

public class InsertSample {

    public static void main(String[] args) {
        //insertA();
        selectA();
    }

    public static void insertA() {
        DataSource ds = ConnectionProperty.getDataSource();
        String sql = "INSERT INTO a (a1, a2, a3, a4) "
                + "VALUES (?, ?, ?, ?)";

        try (Scanner scanner = new Scanner(System.in);
                Connection con = ds.getConnection();
                PreparedStatement pstmt = con.prepareStatement(sql)) {
            System.out.print("a1=");
            String a1 = scanner.nextLine();
            System.out.print("a2=");
            String a2 = scanner.nextLine();
            System.out.print("a3=");
            int a3 = scanner.nextInt();
            System.out.print("a4=");
            double a4 = scanner.nextDouble();

            pstmt.setNString(1, a1);
            pstmt.setNString(2, a2);
            pstmt.setInt(3, a3);
            pstmt.setDouble(4, a4);

            int rowCount = pstmt.executeUpdate();
            System.out.println("插入" + rowCount + "行");

//          System.out.println("a1=" + a1);
//          System.out.println("a2=" + a2);
//          System.out.println("a3=" + a3);
//          System.out.println("a4=" + a4);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void selectA() {
        DataSource ds = ConnectionProperty.getDataSource();
        String sql = "SELECT a1, a2, a3, a4 "
                + "FROM a "
                + "WHERE a3 > ? "
                + "ORDER BY a1";

        try (Scanner scanner = new Scanner(System.in);
                Connection con = ds.getConnection();
                PreparedStatement pstmt = con.prepareStatement(sql)) {

            System.out.print("a3=");
            int a3 = scanner.nextInt();

            pstmt.setInt(1, a3);

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                System.out.print(rs.getNString("a1") + "\t");
                System.out.print(rs.getNString("a2") + "\t");
                System.out.print(rs.getInt("a3") + "\t");
                System.out.println(rs.getDouble("a4"));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

提示

可保存这此Java项目,下次连接可直接导入,只需修改部分信息。

文章永久链接:https://tech.souyunku.com/?p=20027


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(75) 打赏



未经允许不得转载:搜云库技术团队 » 【Java】连接数据库SQLServer

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367