導航:首頁 > 編程語言 > javasql插入

javasql插入

發布時間:2022-11-07 05:00:54

① 用java進行SQL資料庫操作,怎樣插入數據啊

通過用代碼執行了該SQL語句後可以提取你剛剛插進去的數據的逐漸標示值,inset
into

values(???);select
@@identity
這樣的到了這個剛剛插進去的數據的逐漸標示。然後在將這個標示作為你要插入第二章表的數據

② 用JAVA進行SQL資料庫操作,怎樣插入數據啊

通過用代碼執行了該SQL語句後可以提取你剛剛插進去的數據的逐漸標示值,inset into 表 values(???);select @@identity 這樣的到了這個剛剛插進去的數據的逐漸標示。然後在將這個標示作為你要插入第二章表的數據

③ JAVA如何向SQL資料庫中插入記錄

1、連接資料庫,然後寫SQL(insert語句)
2、使用第三方工具(如SPRING、HIBERNATE等),調用相應的插入API

④ java 中如何使用sql插入語句

public int save(Notices notice) throws SQLException{
String sql = "insert into noticeinfo(notice_title,notice_type,notice_content,notice_add_time,user_id,user_table_id,class_table_id,notice_state,is_important)"+"values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = super.getConnection().prepareStatement(sql);
pstmt.setString(1, notice.getNotice_title());
pstmt.setInt(2, notice.getNotice_type());
pstmt.setString(3, notice.getNotice_content());
pstmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
pstmt.setString(5, notice.getUser_id());
pstmt.setInt(6, notice.getUser_table_id());
pstmt.setInt(7, notice.getClass_table_id());
pstmt.setInt(8, notice.getNotice_state());
pstmt.setInt(9, notice.getIs_important());
int i = pstmt.executeUpdate();
return i;
}

⑤ java如何插入數據進sql

(最基本的連接方法)
1。獲取連接
獲取連接需要兩步,
一是使用DriverManager來注冊驅動(Class.forName(「com.mysql.jdbc.Driver」)),二是使用DriverManager來獲取Connection對像DriverManager.getConnection(url,username,password)
2.獲取Statement(Statement stmt =con.createStatement();)
Statement就是執行sql語句的;
3.執行sql語句
String sql = 「insertinto user value(』zhangSan』, 』123』)」;
int m =stmt.executeUpdate(sql);

//總代碼如下

publicstatic Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb1";
return DriverManager.getConnection(url, "root", "123");
}
@Test
publicvoid insert() throws Exception {
Connection con = getConnection();
Statement stmt = con.createStatement();
String sql = "insert into user values('zhangSan', '123')";
stmt.executeUpdate(sql);
System.out.println("插入成功!");
}

⑥ java向sql中添加

import java.sql.*;
class DB()
{
Connection con;
public DB(){
try{
Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver)//動態載入驅動類;
con=DriverManager.getConnection(jdbc:microsoft:sqlserver://localhost:1433;databasename="資料庫名稱","username","password")//鏈接資料庫;
}
}
public boolean insert(要插入的參數)
{
try{
PreparedStatement pstmt=con.prepareStatement("insert into [表名] values(?,?,?)");//values(?,?,?)和要資料庫的欄位對應
pstmt.setString(1,...);
....
pstmt.excuteUpdate();//執行插入
return ture;
}catch(Exception e){e.printStrackTrace();return false;}
}
}

希望對你有幫助

⑦ 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 (num>0)
{
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連sql 插入用數據用for循環,求大神指導。

使用jdbc完成在java文件中添加數據到資料庫。步驟操作:
1建立Connection;
2在for循環下,編寫insert語句;
3建立preparedStatement;
4給sql賦參數值,並調用ps.execute()執行sql,並做異常處理;
5for循環結束,依次關閉ps,connection資源。

⑨ java中執行sql插入語句怎麼弄

1、Connection conn = DriverManager.getConnection(URL,資料庫登錄名,資料庫登錄密碼);//獲得資料庫連接。


2、Statement statement = con.createStatement(); //訪問資料庫。


3、ResultSet resultSet = statement.executeQuery(sql);//執行SQL語句。

閱讀全文

與javasql插入相關的資料

熱點內容
軟通動力程序員節2021 瀏覽:845
安卓系統如何卸載安裝包 瀏覽:870
簡訊刪除助手文件夾 瀏覽:688
java辦公自動化 瀏覽:340
php中超鏈接 瀏覽:253
linux默認路由設置 瀏覽:36
linux如何掛載iso 瀏覽:432
vs程序換文件夾後不能編譯 瀏覽:557
安卓源碼編譯輸入腳本沒反應 瀏覽:47
phpmysql自增 瀏覽:167
把ppt保存為pdf 瀏覽:533
汽車密封件加密配件 瀏覽:887
黑馬程序員15天基礎班 瀏覽:560
java調整格式 瀏覽:521
香港雲伺服器租用價 瀏覽:78
linuxsublime3 瀏覽:560
imac混合硬碟命令 瀏覽:279
沈陽用什麼app租房車 瀏覽:859
00後高中生都用什麼app 瀏覽:239
戴爾塔式伺服器怎麼打開獨立顯卡 瀏覽:808