博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java SqlServer插入操作
阅读量:7081 次
发布时间:2019-06-28

本文共 1175 字,大约阅读时间需要 3 分钟。

hot3.png

package operationsqlserver;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class TestSqlServerMain {

    public static void main(String[] args) throws Exception {
        // 使用sqlserver身份连接
        String urlserver = "jdbc:sqlserver://127.0.0.1:1433;databaseName=TestSql;user=sa;password=123456";

        Connection con = null;

        Statement stmt = null;
        ResultSet rs = null;

        try {

            // 建立连接
            System.out.println("准备连接!!!");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(urlserver);
            System.out.println("连接成功!!!");
            String sql="insert into personinfo (id,name,age) values (?,?,?)";
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setInt(1, 1);
            pstm.setString(2, "zhangsan");
            pstm.setInt(3, 18);
            pstm.executeUpdate();
            System.out.println("插入成功...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                }
            if (stmt != null)
                try {
                    stmt.close();
                } catch (Exception e) {
                }
            if (con != null)
                try {
                    con.close();
                } catch (Exception e) {
                }
        }
    }

}

 

转载于:https://my.oschina.net/u/3197158/blog/911556

你可能感兴趣的文章
同步手绘板——PC端实现画板
查看>>
几种数据库建模工具推荐(包含开源版)
查看>>
Cisco交换机接口模式精解
查看>>
sql查询排序
查看>>
Docker搭建本地私有仓库
查看>>
16TB以后磁盘挂载方法
查看>>
洛谷—— P2895 [USACO08FEB]流星雨Meteor Shower
查看>>
expect 交互式脚本写法
查看>>
cacti程序安装过程
查看>>
学习Linux之路2
查看>>
解决在一行里文字和图片对齐
查看>>
golang hello
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
搭建高可用mongodb集群—— 副本集1
查看>>
分布式助手Zookeeper(一)
查看>>
redis源码分析1------dict的实现
查看>>
Spring HttpIvoker实现Java的远程调用
查看>>
list usage of python
查看>>
某互联网企业技术发展史(一)技术选型与服务器采购
查看>>