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

    原文截图如下:

  • 相关阅读:
    基本类型数组与包装类型数组相互转换
    (转载)JVM中的内存模型与垃圾回收
    Python之threading多线程
    Python之基于socket和select模块实现IO多路复用
    Python之利用socketserver实现并发
    Python的网络编程socket模块
    Python设计模式之一(单例模式)
    Python异常处理
    Python面向对象之常用的特殊方法(5)
    Python面向对象之私有方法(4)
  • 原文地址:https://www.cnblogs.com/360-782/p/5551115.html
Copyright © 2011-2022 走看看