「java添加记录」java添加数据

博主:adminadmin 2023-01-02 09:21:08 1034

今天给各位分享java添加记录的知识,其中也会对java添加数据进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java相数据库中批量添加记录,必须将这些记录都加到list中吗

不一定撒,还是看你的数据是来源是什么。如果是杂乱无序的实际数据,可以在程序里面写到一个list里面,也可以通过用户一组组输入;如果是有规律的数据,那其实可以构造方法产生相关数据,并循环添加入数据库。

看你数据来源。

JAVA怎么向SQL server2000数据库中添加记录

不要搞这个

最好来个

e.printStackTrace();

之类的东西,

或出错提示,

JAVA如何向SQL数据库中插入记录

package users;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.DriverManager;

import java.sql.*;

public class DAO {

Connection conn=null;

PreparedStatement pt=null;

ResultSet rs=null;

int num;

boolean flag=false;

private static final String strcon="sun.jdbc.odbc.JdbcOdbcDriver";

private static final String strman="jdbc:odbc:aaa";

private final String insertSQL="insert into USER values(?,?)";

/**

* 默认构造

*/

public DAO() {

}

/**

* 获得连接

* @return Connection

*/

public Connection Getcon()

{

try {

Class.forName(this.strcon);

}

catch (ClassNotFoundException ex) {

javax.swing.JOptionPane.showMessageDialog(new javax.swing.JButton(),ex.toString());

}

try {

conn = DriverManager.getConnection(this.strman);

}

catch (SQLException ex1) {

javax.swing.JOptionPane.showMessageDialog(new javax.swing.JButton(),ex1.toString());

}

return this.conn;

}

/**

* 添加方法

* @param name String

* @param pass String

* @return boolean

*/

public boolean isinsert(String name,String pass)

{

conn=this.Getcon();

try {

this.pt = conn.prepareStatement(this.insertSQL);

pt.setString(1,name);

pt.setString(2,pass);

num=pt.executeUpdate();

if (num0)

{

flag=true;

}

}

catch (SQLException ex) {

}

return flag;

}

/**

* 关闭方法

*/

public void CloseDB()

{

if (rs!=null)

{

try {

rs.close();

}

catch (SQLException ex) {

javax.swing.JOptionPane.showMessageDialog(new javax.swing.JButton(),ex.toString());

}

}

if (pt!=null)

{

try {

pt.close();

}

catch (SQLException ex1) {

javax.swing.JOptionPane.showMessageDialog(new javax.swing.JButton(),ex1.toString());

}

}

if (conn!=null)

{

try {

conn.close();

}

catch (SQLException ex2) {

javax.swing.JOptionPane.showMessageDialog(new javax.swing.JButton(),ex2.toString());

}

}

}

}

public static void main(String[] args)

自己配一下 

貌似有点麻烦

以后项目方便

java 如何连接数据库,如何完成数据库记录添加,删除,修改以及查询。

其实,也就那几步而已:

别忘了,加载jar文件

加载驱动

获得Connection

获得PreparedStatement /PreparedStatement

执行sql语句

处理结果集ResultSet

关闭连接

mport java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import Entity.JIAJU;

public class JiaJu {

 public JIAJU selectExe(int shouhinId) {

  JIAJU jia = new JIAJU();

  try {

   Connection con = ConnectionManager.getConnection();

   String sql = "select * from jiaju where shouhinId=?";

   PreparedStatement ps = con.prepareStatement(sql);

   ps.setInt(1, shouhinId);

   ResultSet rs = ps.executeQuery();

   if (rs != null) {

    while (rs.next()) {

     jia.setShouhinId(rs.getInt("shouhinId"));

     jia.setShouhinName(rs.getString("shouhinName"));

     jia.setShouhinColor(rs.getString("shouhinColor"));

     jia.setShouhinPrice(rs.getInt("shouhinPrice"));

     jia.setShouhinPai(rs.getString("shouhinPai"));

     jia.setShouhinShi(rs.getString("shouhinShi"));

     // list.add(jia);

    }

   }

  } catch (Exception e) {

   e.printStackTrace();

  }

  return jia;

 }

 public void insertJia(JIAJU jia) {

  try {

   Connection con = ConnectionManager.getConnection();

   String sql = "insert into jiaju values(?,?,?,?,?)";

   PreparedStatement ps = con.prepareStatement(sql);

   ps.setString(1, jia.getShouhinName());

   ps.setString(2, jia.getShouhinColor());

   ps.setInt(3, jia.getShouhinPrice());

   ps.setString(4, jia.getShouhinPai());

   ps.setString(5, jia.getShouhinShi());

   ps.executeUpdate();

  } catch (Exception e) {

   e.printStackTrace();

  }

 }

 public List selectJia() {

  List list = new ArrayList();

  

  try {

   Connection con = ConnectionManager.getConnection();

   String sql = "select * from jiaju ";

   PreparedStatement ps = con.prepareStatement(sql);

   ResultSet rs = ps.executeQuery();

   if (rs != null) {

    while (rs.next()) {

     JIAJU jia = new JIAJU();

     jia.setShouhinId(rs.getInt("shouhinId"));

     jia.setShouhinName(rs.getString("shouhinName"));

     jia.setShouhinColor(rs.getString("shouhinColor"));

     jia.setShouhinPrice(rs.getInt("shouhinPrice"));

     jia.setShouhinPai(rs.getString("shouhinPai"));

     jia.setShouhinShi(rs.getString("shouhinShi"));

        list.add(jia);

    }

   }

  } catch (Exception e) {

   e.printStackTrace();

  }

  return list;

 }

 public JIAJU selectbuy(int shouhinId) {

  JIAJU jia = new JIAJU();

  try {

   Connection con = ConnectionManager.getConnection();

   String sql = "select * from jiaju where shouhinId=?";

   PreparedStatement ps = con.prepareStatement(sql);

   ps.setInt(1, shouhinId);

   ResultSet rs = ps.executeQuery();

   if (rs != null) {

    while (rs.next()) {

     jia.setShouhinId(rs.getInt("shouhinId"));

     jia.setShouhinName(rs.getString("shouhinName"));

     jia.setShouhinColor(rs.getString("shouhinColor"));

     jia.setShouhinPrice(rs.getInt("shouhinPrice"));

     jia.setShouhinPai(rs.getString("shouhinPai"));

     jia.setShouhinShi(rs.getString("shouhinShi"));

    }

   }

  } catch (Exception e) {

   e.printStackTrace();

  }

  return jia;

 }

  public void updateLou(JIAJU jia){

   try{

    Connection con = ConnectionManager.getConnection();

   String sql = "update jiaju set shouhinPrice=? where  shouhinId=?";

   PreparedStatement ps = con.prepareStatement(sql);  

   ps.setInt(1,jia.getShouhinPrice());

   ps.setInt(2, jia.getShouhinId());

   ps.executeUpdate();

   }catch(Exception e){

    e.printStackTrace();

   }

  }

  public void deleteLou(JIAJU jia){

   try{

    Connection con = ConnectionManager.getConnection();

   String sql = "delete from jiaju where shouhinId=?";

   PreparedStatement ps = con.prepareStatement(sql);

   ps.setInt(1, jia.getShouhinId());

   ps.executeUpdate();

   }catch(Exception e){

    e.printStackTrace();

   }

  }

}

做java项目时在代码中添加日志怎样的添加形式比较好?

异步线程写入日志,这样既不会影响业务代码,也不会影响系统性能,像你直接在主线程添加记录日志方法,如果写入比较慢的话,甚至会导致用户使用正常功能延迟而产生页面请求超时。

关于java添加记录和java添加数据的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。