码迷,mamicode.com
首页 > 数据库 > 详细

Java连接数据库之SQLServer

时间:2018-11-28 22:14:15      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:into   --   sql   文件   stack   coding   rate   from   com   

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

使用方法:右键点击项目,选择 Buiding Path | Configure Buiding Path | Add Exteral JARs 选择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项目,下次连接可直接导入,只需修改部分信息。

Java连接数据库之SQLServer

标签:into   --   sql   文件   stack   coding   rate   from   com   

原文地址:https://www.cnblogs.com/blknemo/p/10034496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!