Java 操作 SQL Server

导入sql server 驱动

1.项目下新建lib文件夹
2.微软官网下载sqljdbc_x.x.0.0_chs.zip并解压
3.将对应jar拷贝到lib文件夹
4.右键jar文件添加到项目构造路径

Eclipse 快速生成标准输出语句

//输入下段短语并使用智能提示快捷键:Alt+/
sysout

代码

import java.sql.*;
public class MyJdbc_01 {
        //四大金刚
    static String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    static String url = "jdbc:sqlserver://10.130.1.11:1433;DatabaseName=test_data_yinyl";
    static String user = "sa";
    static String password = "****";
    public static void main(String[] args) {
        //三大对象
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        int affectRows = 0; 
        try {
            Class.forName(driverClassName);
        }
        catch (ClassNotFoundException e) {
            // TODO: handle exception
            System.out.println("类名["+driverClassName+"]未找到");
        }
        try {
            //创建一个新数据库对象
            connection = DriverManager.getConnection(url, user,password);
            statement = connection.createStatement();
            //新建一个查询操作
            String sqlString = "select * from t0 where id < 8";
            resultSet = statement.executeQuery(sqlString);
            while (resultSet.next()) {
                int tmp = resultSet.getInt("id");
                System.out.println(tmp);    //依次打印查询结果中的id值         
            }
            //执行一条插入操作
//          affectRows = statement.executeUpdate("insert into t0 values('t','t',default)");
            //执行一条删除操作
            affectRows = statement.executeUpdate("delete from t0 where id >12 and id < 43");
            //执行一条更新操作
//          System.out.println("正在尝试将id为3处的设备名称更新为设备1");
//          affectRows = statement.executeUpdate("update t0 set dev = '设备1' where id = 3");
            //打印受影响行数
            System.out.println(affectRows+"行受影响");
        } catch (SQLException e) { 
            // TODO: handle exception
            e.printStackTrace();
        }

    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇