當前位置:首頁 » 編程語言 » java解析sql

java解析sql

發布時間: 2022-09-24 01:33:24

java 正則表達式 解析sql文的表名,有半成品,求改進

試試這個


(?i)(?<=(?:from|into|update|join|)s*)w*[.]?w*(?=s*)

❷ java如何執行sql語句

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;


public class xxxx {

public static void main(String[] args) {

Connection con = null ;

Statement stmt = null ;

try {

Class.forName("com.mysql.jdbc.Driver"); //mysql為例 不一樣的資料庫所需的驅動包不一樣 連接語句略有不同

con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/資料庫名", "root", "密碼");

stmt = con.createStatement();

String sql = "insert into info values ('用戶', 'mima', 'piapiapia~')";

stmt.executeUpdate(sql);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if(stmt != null) {

stmt.close();

stmt = null;

}

if (con != null) {

con.close();

con = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}


}


❸ java解析sql語句是實現不了的

這個需要用到編譯原理的知識,理論上,既然dbms可以解析,那我們當然可以實現。
不過比較、很、太難實現而已。
並且不同資料庫、不同版本的語法結構還不一樣,又進一步增加了難度

❹ 無法解析java.sql.Connection

不是那些原因,是你的單詞打錯了,要細心點!是java.sql.Connection;和java.sql.Drivermanager;沒有其他原因!

❺ java將json數據解析為sql語句

importjava.util.Iterator;
importjava.util.Set;
importjava.util.Map.Entry;

importcom.google.gson.JsonArray;
importcom.google.gson.JsonElement;
importcom.google.gson.JsonObject;
importcom.google.gson.JsonParser;

publicclassSql
{
publicstaticStringparseSQL(Stringjson)
{
JsonParserparser=newJsonParser();
JsonObjectobj=(JsonObject)parser.parse(json);;
Stringtable=obj.get("table").getAsString();
Stringop_type=obj.get("op_type").getAsString();
Stringsql="";
if("I".equals(op_type))
{
sql+="INSERTINTO"+table+"(";
JsonObjectafter=(JsonObject)obj.get("after");
Set<Entry<String,JsonElement>>entry=after.entrySet();
Iterator<Entry<String,JsonElement>>it=entry.iterator();
Stringvs="values(";
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+",";
vs+=val+",";
}
sql=sql.replaceAll(",\s*$","");
vs=vs.replaceAll(",\s*$","");
sql+=")"+vs+")";
}
elseif("U".equals(op_type))
{
sql+="UPDATE"+table+"SET";
JsonObjectafter=(JsonObject)obj.get("after");
Set<Entry<String,JsonElement>>entry=after.entrySet();
Iterator<Entry<String,JsonElement>>it=entry.iterator();
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+"="+val+",";
}
sql=sql.replaceAll(",\s*$","");
sql+="WHERE";
after=(JsonObject)obj.get("before");
entry=after.entrySet();
it=entry.iterator();
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+"="+val+"AND";
}
sql=sql.replaceAll("\s*AND\s*$","");
}
elseif("D".equals(op_type))
{
sql+="DELETEFROM"+table+"WHERE";
JsonObjectafter=(JsonObject)obj.get("before");
Set<Entry<String,JsonElement>>entry=after.entrySet();
Iterator<Entry<String,JsonElement>>it=entry.iterator();
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+"="+val+"AND";
}
sql=sql.replaceAll("\s*AND\s*$","");
}
returnsql;
}

publicstaticvoidmain(String[]args)
{
Stringinsert=
"{"table":"GG.TCUSTORD","op_type":"I","op_ts":"2013-06-0222:14:36.000000","current_ts":"2015-09-18T13:39:35.447000","pos":"00000000000000001444","tokens":{"R":"AADPkvAAEAAEqL2AAA"},"after":{"CUST_CODE":"WILL","ORDER_DATE":"1994-09-30:15:33:00","PRODUCT_CODE":"CAR","ORDER_ID":"144","PRODUCT_PRICE":17520.00,"PRODUCT_AMOUNT":3,"TRANSACTION_ID":"100"}}";
Stringupdate=
"{"table":"GG.TCUSTORD","op_type":"U","op_ts":"2013-06-0222:14:41.000000","current_ts":"2015-09-18T13:39:35.748000","pos":"00000000000000002891","tokens":{"L":"206080450","6":"9.0.80330","R":"AADPkvAAEAAEqLzAAC"},"before":{"CUST_CODE":"BILL","ORDER_DATE":"1995-12-31:15:00:00","PRODUCT_CODE":"CAR","ORDER_ID":"765","PRODUCT_PRICE":15000.00,"PRODUCT_AMOUNT":3,"TRANSACTION_ID":"100"},"after":{"CUST_CODE":"BILL","ORDER_DATE":"1995-12-31:15:00:00","PRODUCT_CODE":"CAR","ORDER_ID":"765","PRODUCT_PRICE":14000.00,"PRODUCT_AMOUNT":3,"TRANSCATION_ID":"100"}}";
Stringdelete=
"{"table":"GG.TCUSTORD","op_type":"D","op_ts":"2013-06-0222:14:41.000000","current_ts":"2015-09-18T13:39:35.766000","pos":"00000000000000004338","tokens":{"L":"206080450","6":"9.0.80330","R":"AADPkvAAEAAEqLzAAC"},"before":{"CUST_CODE":"DAVE","ORDER_DATE":"1993-11-03:07:51:35","PRODUCT_CODE":"PLANE","ORDER_ID":"600"}}";
System.out.println(parseSQL(insert));
System.out.println(parseSQL(update));
System.out.println(parseSQL(delete));
}
}

❻ java 怎麼用正則表達式解析sql中的表名,已有半成品,求改善

Stringstr="from\s+(.*)\s+where?";
Stringsql="select*fromtable,table2wherea=b";
Patternp=Pattern.compile(str);
Matchermatcher=p.matcher(sql);
while(matcher.find()){
Stringstring=matcher.group(1);
System.out.println(string);
}

❼ java解析字元串(解析sql語句,得到需要查詢顯示的欄位名)。

在Sql語句前加上SET FMTONLY ON,再執行Sql語句,該設置只將元數據返回給客戶端,可以用於測試響應的格式,而不必實際執行查詢.有了返回的表結構,還怕得到不到要查詢的欄位
例:
SET FMTONLY ON;
SELECT f_spbm,f_spmc as '名稱' FROM 表1;
SET FMTONLY OFF;

❽ 使用java jdbc方法查詢資料庫 ora sql 報錯 無效的列類型 但是使用java 解析出來的sql 去資料庫查執行成

select LG_TNAME from SYS_CHANGHIS_SET where DB_CLASS=?];
這個SQL有問題 Object args=new Object[]{dbclass};這個變數需要正確聲明

❾ Java語言中sql語句

import java.sql.* ;
public class testDB
{
public static void main(String []args)
{
String DBDRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
String DBURL = "jdbc:odbc:ttt";
String sql = "CREATE TABLE aTable(colum1 varchar(20) ,colum2 integer);" ;
PreparedStatement pstmt = null ;
Connection conn = null ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL) ;
pstmt = conn.prepareStatement(sql) ;
pstmt.execute() ;
pstmt.close() ;
conn.close() ;
}
catch (Exception e)
{}
}
};

熱點內容
ibatis生成sql 發布:2025-01-10 03:56:10 瀏覽:517
我的表姐迪克電腦密碼多少 發布:2025-01-10 03:27:40 瀏覽:766
主機訪問P 發布:2025-01-10 03:17:09 瀏覽:755
滴滴出行腳本 發布:2025-01-10 03:17:03 瀏覽:743
安卓扁口有線耳機哪個好 發布:2025-01-10 03:12:06 瀏覽:643
cubemx中的時鍾如何配置 發布:2025-01-10 03:09:51 瀏覽:726
電腦頁面怎麼設置密碼 發布:2025-01-10 03:05:41 瀏覽:878
mp4加密提取 發布:2025-01-10 03:05:39 瀏覽:838
我的世界伺服器地址後綴 發布:2025-01-10 02:55:40 瀏覽:522
百分30利潤怎麼演算法 發布:2025-01-10 02:47:26 瀏覽:964