❶ 用java訪問資料庫 判斷數據是否存在
sql方面的寫法:
select count(*) from 訂單表 where 訂單號='你傳進來的經單號'
java代碼中返回這個count
if(count >= 1) 說明訂單已存在!!!!
--希望能幫助到你!
❷ java中怎麼查詢表單是否存在
1.直接對資料庫表進行操作,如查詢操作,資料庫表不存在則會拋出異常。如果收到異常則可以進行後續的建表操作了。
2.讀取資料庫中表名列表,看錶是否在其中,代碼如下:
//java獲取資料庫中所有表名,判斷某個表是否在資料庫中存在
connection = DriverManager.getConnection(mySqlDbUrl, "", ""); DatabaseMetaData meta = (DatabaseMetaData)connection.getMetaData(); ResultSet rs = meta.getTables(null, null, "table", null);//table為表名 if(rs.next()){ System.err.println(true); }else{ System.err.println(false); } rs.close(); connection.close();
❸ java 資料庫問題 怎麼判斷要插入的主鍵數據是否在資料庫表中已存在了!
先用主鍵去選數據
如果數據存在的話,不就已經存在
不能插入了
❹ java語言如何判斷oracle是否存在某張表急,求大神指點啊!!
public boolean validateTableExist(String tableName){
int result = 0;
String sql = "SELECT COUNT(*) FROM USER_OBJECTS WHERE OBJECT_NAME = UPPER('"+tableName+"')";
Connection conn = this.getSession().connection();
Statement st = null;
ResultSet rs = null;
try{
st = conn.createStatement();
rs = st.executeQuery(sql);
rs.next();
result = rs.getInt(1);
}catch(Exception e){
e.printStackTrace();
}finally{
this.util.closeAll(rs, st, conn);
}
return result==0?false:true;
}
JDBC的完整方法,CloseAll方法就不給你寫了.如果表存在就返回True,不存在返回False,可以直接用在你的IF判斷中:
if(validateTableExist(tname)){
String sql1="insert into "+tname+" values(?,?,?,?,?,?)";
....................
}
❺ 用java判斷資料庫某個表是不是存在
1、sql語句判斷資料庫表是否存在:
sql:select * from user_all_tables where table_name='tableName'
如果結果為空則表示不存在,如何結果不為空則表示存在;
2、java如何判斷資料庫表是否存在
可以利用上面的sql,執行獲取結果,相應的java代碼如下:
String helperName= delegator.getGroupHelperName("com.asiainfo");
SQLProcessor sqlProcessor= new SQLProcessor(helperName);
String sql = "select * from user_all_tables where table_name='"+table+"'";
ResultSet rsTables =sqlProcessor.executeQuery(sql);
if(rsTables.next()){
Debug.logWarning("table:"+table+" exists", mole);
}else{
Debug.logWarning("table:"+table+" does not exist", mole);
}
❻ [轉載]java和sql如何判斷資料庫表是否存在
1.sql語句判斷資料庫表是否存在: sql:select * from user_all_tables where table_name='tableName' String helperName= delegator.getGroupHelperName("com.asiainfo"); SQLProcessor sqlProcessor= new SQLProcessor(helperName); String sql = "select * from user_all_tables where table_name='"+table+"'"; ResultSet rsTables =sqlProcessor.executeQuery(sql); if(rsTables.next()){ Debug.logWarning("table:"+table+"exists", mole);}else{ Debug.logWarning("table:"+table+" does not exist", mole);}方法二:DatabaseMetaData meta = m_sqlCon.getMetaData(); ResultSet rsTables = meta.getTables(null , null, 「YourTableName」, null); if(rsTables.next()){ System.out.println("The Table exsits.");}else{ System.out.println("The 如果schema參數為null的話,那麼它會查詢整個資料庫中的表有可能會沖突的: getTables(String catalog,String schemaPattern,String tableNamePattern,String[] types) 參數: catalog:目錄名稱,一般都為空. 參數:schema:資料庫名,對於oracle來說就用戶名 參數:tablename:表名稱 參數:type :表的類型(TABLE | VIEW) 注意:在使用過程中,參數名稱必須使用大寫的。
❼ java如何判斷資料庫表是否存在,又如何創建新表
首先,數據表存不存在這是屬於資料庫的范疇,跟java沒有必然聯系,你說的java或者jdbc來判斷,最終也是調用的sql語句來判斷的。
判斷數據表存不存在是用sql語句來判斷的,不同的資料庫,其判斷的方式有些不一樣,
比如oralce、mysql資料庫 你可以用create table if not exists 這個語法句式來創建表。
❽ 怎麼在java中判斷一個資料庫中是否存在表user
方法有二:
1、查詢表user,如果拋出表不存在的異常,就證明表user不存在。
2、查詢系統表,每種資料庫都有一張系統表,用該存放資料庫中的所有表的信息。
你只需要查詢系統表有無該表的記錄即可。但是不同種類的資料庫,系統表可能不一樣。
❾ 如何在JAVA 中的PreparedStatement 語句中來判斷一個資料庫表是否存在
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import util.DBUtil;
public class TableExistTest {
public static void main(String[] args) {
String tableName = "test1";
Connection con = null;
ResultSet rs = null;
PreparedStatement ps = null;
String sql = "SELECT NAME FROM sysobjects WHERE NAME='" + tableName
+ "'";
System.out.println(sql);
try {
con = DBUtil.getConnection();//取得資料庫連接
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
System.out.println("存在" + tableName);
} else {
System.out.println("不存在" + tableName);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
附加提示:
你在查詢分析器裡面執行這條語句,你會很興奮
SELECT * FROM sysobjects
❿ java判斷資料庫的表裡面是否存在一個值
一個SQL語句不就完了,select count(*) from MAA where NO=『1234-4567-8910』。看count的值是否是0,不是0 則列印這個值存在。難道前邊連接資料庫的也要