zoukankan      html  css  js  c++  java
  • JavaEE学习笔记---数据库操作篇

    测试JDBC和SQLServer的插入操作,源码如下:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Scanner;


    /**
    * @author 墨虺
    *
    */

    public class DBDemoInsert {
    public static void main(String[] args) throws Exception{
    String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    Class.forName(driver);
    /*
    String url="jdbc:sqlserver://127.0.0.1:1433;database=ssh";
    Connection con=DriverManager.getConnection(url,"sa","fyl360782");
    */
    String url="jdbc:sqlserver://127.0.0.1:1433;database=ssh;user=*******;password=******";
    Connection con=DriverManager.getConnection(url);

    String sql="select * from subscriber";
    Statement cmd=con.createStatement();
    ResultSet rs=cmd.executeQuery(sql);

    System.out.println("账户 "+"密码 "+"昵称 "+"电话 ");
    while(rs.next()){
    String userid=rs.getString(1);
    String pwd=rs.getString(2);
    String name=rs.getString(3);
    String phone=rs.getString(4);
    System.out.println(userid+" "+pwd+" "+name+" "+phone);
    }



    String insertsql="insert into users(userid,pwd,name,phone) values(?,?,?,?)";
    PreparedStatement insertcmd=con.prepareStatement(insertsql);

    System.out.println("按照如右格式输入数据,空格分隔,回车确认:"+" 账户 "+"密码 "+"昵称 "+"电话:");
    Scanner reader=new Scanner(System.in);
    System.out.println(reader.next()+" "+reader.next()+" "+reader.next()+" "+reader.next());

    insertcmd.setString(1,"raisann");
    insertcmd.setString(2,"asd");
    insertcmd.setString(3,"らいさん");
    insertcmd.setString(4,"6698258");

    insertcmd.executeUpdate();
    /*
    insertcmd.setString(1,reader.next());
    insertcmd.setString(2,reader.next());
    insertcmd.setString(3,reader.next());
    insertcmd.setString(4,reader.next());

    insertcmd.executeUpdate();
    */
    reader.close();
    con.close();
    }
    }

    进行数据库Insert操作发生如下错误:

    必应搜索后找到如下解决方案:

    Thanks Mr. Normand for your continued efford to solve my problem. I have found the error..

    In fact what mistake I was doing that I was searching for the class as usual in com.microsoft.sqlserver.jdbc . However in the error (that I attached) first line said 'java.lang.NoClassDefFoundError: microsoft/sql/DateTimeOffset'. It means It was trying to find the class in microsoft/sql directory. After creating the directory in my Classpath it worked.

    There are only two classes under microsoft/sql viz. Types and DateTimeOffset. The reason best known to Microsoft only.
    Now it is working. This may be helpful to others too. Further it works with SQLServer 2005 successfully.

    Thanks..

    Kundu

    原文截图如下:

  • 相关阅读:
    class(类)和构造函数(原型对象)
    es6中export和export default的区别
    vue混入 (mixin)的使用
    ES6(Module模块化)
    vue-cli3构建和发布 实现分环境打包步骤(给不同的环境配置相对应的打包命令)
    Vue中使用Echarts 脱坑
    Nginx配置详解
    VUE面包屑组件
    更改 pip 默认下载源(pip 配置文件)
    常见免费API接口
  • 原文地址:https://www.cnblogs.com/360-782/p/5551115.html
Copyright © 2011-2022 走看看