「java数据库填充数据」java数据库填充数据怎么填

博主:adminadmin 2022-11-25 16:46:07 47

本篇文章给大家谈谈java数据库填充数据,以及java数据库填充数据怎么填对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 数据库插入超大数据怎么处理

1、加大Java可使的内存量、单次提交的条数再扩大些、扩大java连接数据库最大连接数都可以从量上来解决这个问题,但是想质的解决这样做是不好的。

2、用存储过程,来解决大量数据的CRUD是最明智的选择。写个存储过程,把数据一千条或几条的传给存储过程,由存储过程去解析,然后CRUD就能从本质上解决这个问题了。

这也是为何电信运营商的数据库应中,都是大量的存储过程即pl/sql的原因了。

java 向数据库插入数据

java向数据库中插入数据,可以使用mysql数据库,使用statement类来操作数据库,示例如下:

Connection conn = null;

  Statement st = null;

  try {

   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载驱动类

   conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://server_name:1433", "name","pwd");

   conn.setAutoCommit(false);

   st = conn.createStatement();

   // 模拟一个 str[i] = nd.getNodeValue().trim()

   String[] str = new String[] { "aaa", "bbb", "ccc", "ddd", "eee","fff" };

   String sqlStr = null;

   for (int i = 0; i  str.length; i++) {

    sqlStr = "INSERT INTO TABLENAME (COLNAME)VALUES('" + str[i] + "')";//向数据库中插入数据

    st.executeUpdate(sqlStr);

   }

   conn.commit();

  } catch (Exception e) {

   e.printStackTrace();

  } finally {//释放数据库的资源

   try {

    if (st != null)

     st.close();

    if(conn != null  !conn.isClosed()){

     conn.close();

    }

   } catch (SQLException e) {

    e.printStackTrace();

   }

  }

如何用Java向数据库中添加数据?

1.提取单条记录

//import java.sql.*;

Connection conn=null;

Statement stmt=null;

ResultSet rs=null;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url="jdbc:odbc:%%1";

con=DriverManager.getConnection(url,%%2,%%3);

stmt=conn.createStatement();

stmt.executeUpdate(%%4);

rs=stmt.executeQuery(%%5);

}catch(Exception e){

e.printStackTrace();

}

finally{

try {

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

if(conn!=null)

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

3.显示表格

/*

import java.awt.*;

import javax.swing.*;

import java.sql.*;

import javax.swing.table.*;

*/

String[] colHeads=%%4;

Connection conn=null;

Statement stmt=null;

ResultSet rs=null;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url="jdbc:odbc:%%1";

conn=DriverManager.getConnection(url,%%2,%%3);

stmt=conn.createStatement();

rs=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);

rs.next();

int iCount=rs.getInt("au_count");

Object[][] data=new Object[iCount][];

int i=0;

rs=stmt.executeQuery("SELECT * from "+%%5);

while(rs.next()){

data[i]=new Object[iCount];

data[i][0]=rs.getString("au_fname");

data[i][1]=rs.getString("Phone");

data[i][2]=rs.getString("City");

i++;

}

JTable table=new JTable(data,colHeads);

JScrollPane jsp=new JScrollPane(table);

getContentPane().add(jsp);

}catch(Exception e){

e.printStackTrace();

}

finally{

try {

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

if(conn!=null)

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

6.关闭时关闭连接

//import java.sql.*;

addWindowListener(new WindowAdapter{

public void windowClosing(WindowEvent wevent){

if(stmt!=null){

try {

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

if(conn!=null)

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

7.执行命令

//import java.sql.*;

Connection conn=null;

PreparedStatement pst=null;

try {

conn=DriverManager.getConnection(url);

pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");

pst.setInt(1,%%2);

//pst.setString(2,%%2);

pst.addBatch();

pst.executeBatch();

} catch (SQLException e){

e.printStackTrace();

}

finally{

try {

if (pst != null)

pst.close();

if (conn != null)

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

java中往数据库表中添加data数据

只能写个大概的,要写数据到数据库中,先得在数据库中建库,库里建表,表里建字段,然后java里建立数据库连接,用SQL语言写数据到表中的字段

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名"; //7.0、2000

String url="jdbc:sqlserver://localhost:1433;DatabaseName=数据库名"; //2005

Connection conn=null;

conn= DriverManager.getConnection(url,用户名,密码);

PreparedStatement pst=null;

pst=conn.prepareStatement("Insert Into grade(表名) Values (?)");

pst.setInt(1,你要写的整弄数据);

//pst.setString(2,你要写的字符串数据);

pst.addBatch();

pst.executeBatch();

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

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。