① 用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語句。