當前位置:首頁 » 數據倉庫 » java資料庫數據錄入
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java資料庫數據錄入

發布時間: 2023-02-05 19:58:29

1. 用java怎樣把數據存到資料庫

只能寫個大概的,要寫數據到資料庫中,先得在資料庫中建庫,庫里建表,表裡建欄位,然後java里建立資料庫連接,用sql語言寫數據到表中的欄位x0dx0aClass.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); x0dx0a//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=資料庫名"; //7.0、2000x0dx0aString url="jdbc:sqlserver://localhost:1433;DatabaseName=資料庫名"; //2005x0dx0aConnection conn=null;x0dx0aconn= DriverManager.getConnection(url,用戶名,密碼); x0dx0aPreparedStatement pst=null;x0dx0apst=conn.prepareStatement("Insert Into grade(表名) Values (?)");x0dx0apst.setInt(1,你要寫的整弄數據);x0dx0a//pst.setString(2,你要寫的字元串數據);x0dx0apst.addBatch();x0dx0apst.executeBatch();

2. java中怎麼向資料庫插入數據

Java程序向資料庫中插入數據,代碼如下:

//首先創建資料庫,(access,oracle,mysql,sqlsever)其中之一,其中access,sqlsever需要配置數據源(odbc);
//然後再eclipse中創建類(ConnDb,Test,TestBean)ConnDb功能為連接資料庫,查詢,插入,刪除,修改數據的類,Test為含有main方法的測試類,TestBean為數據表中的欄位屬性及set,get方法
//以下是ConnDb代碼:
packagedb;
importjava.sql.Connection;
importjava.sql.DriverManager;
import
java.sql.ResultSet;
importjava.sql.SQLException;
import
java.sql.Statement;
importjava.util.ArrayList;
publicclassConnDb{
publicConnectionstartConn(Connectionconn){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:資料庫","用戶名","密碼");
}catch(Exceptione){
System.out.println("連接資料庫時出現錯誤");
}
returnconn;
}

publicArrayListexecuteQuery(Stringsql){
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
ArrayListlist=newArrayList();
try{
conn=startConn(conn);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);//sql為sql語句例如"select*from
表名",從main方法中傳進來,這里用的是ArrayList類將查詢結果存儲起來
while(rs.next()){
TestBeantb=newTestBean();
tb.setTid(rs.getString("tid"));
tb.setTname(rs.getString("tname"));
tb.setTinfo(rs.getString("tinfo"));
list.add(tb);
}
}
catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
closeConn(rs,stmt,conn);
}
returnlist;
}
publicvoidexecuteUpdate(Stringsql){
Connectionconn=null;
Statementstmt=null;
try{
conn=
startConn(conn);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLExceptione){
System.out.println("修改,插入或者刪除資料庫數據時發生錯誤!");
}finally{
closeConn(stmt,conn);
}
}
publicvoidcloseConn(ResultSetrs,Statementstmt,Connectionconn){
try{
if(rs!=
null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
catch(SQLExceptione){
//TODOAuto-generatedcatch
block
System.out.println("關閉資料庫的時候發生錯誤!");
}
}
publicvoidcloseConn(Statementstmt,Connectionconn){
try{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
catch(SQLExceptione){
//TODOAuto-generatedcatchblock
System.out.println("關閉資料庫的時候發生錯誤!");
}
}
}

3. java程序:怎麼把注冊的數據錄入資料庫

根本來講需要程序寫入資料庫,最後肯定要到 sql等資料庫語句上面。可以自己寫,也可以用一些成熟的框架,比如hibernate

4. 如何用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();
}
}

5. java中怎麼實現批量錄入數據

批量數據進入資料庫使用addBatch()和executeBatch()方法

PreparedStatement.addBatch();
......
PreparedStatement.executeBatch();
需要注意的是一次最多不要超過50條:
1.因為插入的時候資料庫已經鎖定,然而若是一次性插入太多會造成其他業務的等待。
2.會造成內存的溢出

舉例:

PreparedStatementpst=(PreparedStatement)con.prepareStatement("insertinto*****values(?,'***')");
for(inti=0;i<10000;i++){
pst.setInt(1,i);
//把一個SQL命令加入命令列表
pst.addBatch();
}
//執行批量更新
pst.executeBatch();
//語句執行完畢,提交本事務
con.commit();

資料來源:CSDN論壇-原資論壇料鏈接如下

URLSRC:http://bbs.csdn.net/topics/320048117

6. 如何實現用java程序在前台對後台sqlserver 資料庫錄入數據謝謝

一個完整的應用程序,本身就要有一個前台,一個後台,首先,要建立數據源 建立之後建立一個JAVA工程,建立一個JAVA類,寫下如下代碼
Package ....;
import java.sql.*;
Public Class Database{
Public Database(){
}
Connection con;
Statement st;
ResultSet rs;
try{
Class.forname("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){
System.out.println(e);
}
try{
con = DriverManager.getconnection(Jdbc:Odbc:數據源名","密碼","");
st = con.CreateStatement();
int value = st.updateQuery(寫入insert或update語句);
System.out.println(value);
}
catch(Exception e){
System.out.println(e);
}
con.close();
}
如果輸出的value值為1說明操作成功。

Class.forname()//載入驅動的

7. java 向資料庫插入數據

java向資料庫中插入數據,可以使用mysql資料庫,使用statement類來操作資料庫,示例如下:

Connectionconn=null;
Statementst=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=newString[]{"aaa","bbb","ccc","ddd","eee","fff"};
StringsqlStr=null;
for(inti=0;i<str.length;i++){
sqlStr="INSERTINTO<TABLENAME>(<COLNAME>)VALUES('"+str[i]+"')";//向資料庫中插入數據
st.executeUpdate(sqlStr);
}
conn.commit();
}catch(Exceptione){
e.printStackTrace();
}finally{//釋放資料庫的資源
try{
if(st!=null)
st.close();
if(conn!=null&&!conn.isClosed()){
conn.close();
}
}catch(SQLExceptione){
e.printStackTrace();
}
}

8. Java怎麼向資料庫中插入數據

SQL語句有問題。你在下面輸出SQL看下就明白了。

應改為:

String sql="insert into aa(a,b,c) values('"+h+"','"+f+"','"+g+"')";

9. java編程中怎麼向資料庫中添加數據啊(數據是未知的等待用戶輸入的)用戶輸入的數據為數字型,不是文本型

看你是什麼資料庫了,JAVA的JDBC連接資料庫需要驅動包,這個你可以到網上搜索,對於不同的資料庫使用不同的驅動,可以使用簡單的SQL語句來完成,insert into data(NO,name,sex,age,class,tel,address,post) values(NO,name,sex,age,class,tel,address,post)其中data是資料庫中的表NO,name,sex,age,class,tel,address,post這些是資料庫中的欄位values(NO,name,sex,age,class,tel,address,post)這個是獲得所填入表單中的值。

10. 我想用java寫個簡單的程序,把數據輸入到資料庫中,麻煩大家提供個簡單的程序供我學習一下吧~謝謝~

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class A {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//連接Mysql
Class.forName("com.mysql.jdbc.Driver");
Connection conn2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/ttt", "root", "root");

String sql = "insert into factory values(0,?,?)";
PreparedStatement pstmt = conn2.prepareStatement(sql);

//填充參數
pstmt.setObject(1, "haha");
pstmt.setObject(2, "heihei");

// 執行
int c = pstmt.executeUpdate();
}
}

這是一個往資料庫中插入數據的代碼,碼字不容易