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

    原文截图如下:

  • 相关阅读:
    一个很老的故事: 水和鱼的故事
    签到
    WEBPART结合实际的应用(.Net2005)
    Silverlight 3正式版新鲜出炉
    一个端口扫描的小程序
    带验证功能的的TextBox
    php解析url并得到url中的参数及获取url参数的四种方式
    Mysql外键约束设置使用方法
    require(): open_basedir restriction in effect. 解决方法
    PHP将变量存储在数据库中,读取并执行变量的方法
  • 原文地址:https://www.cnblogs.com/360-782/p/5551115.html
Copyright © 2011-2022 走看看