java解析sql
❶ 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)
{}
}
};