怎么用java实现驼峰命名法与数据库命名法的相互转换

怎么用java实现驼峰命名法与数据库命名法的相互转换,第1张

手敲的,给个辛苦分吧。。。

package test;

import javautilArrayList;

import javautilList;

public class Test {

public static void main(String[] args) {

//testing for data base column-key to java field

String[] dbKeys = {"id", "user_age", "user_addr_"};

Test t = new Test();

tconvertToJava(dbKeys);

Systemoutprintln("-----------------------------------");

//testing for Java field to data base column-key

String javaFieldNames[] = {"id","userAge","userHomeAddr"};

tgetDBKey(javaFieldNames);

}

/

  Java field to data base column-key

 /

private void getDBKey(String javaFieldNames){

if(javaFieldNames != null && javaFieldNameslength > 0){

for(String name : javaFieldNames){

StringBuffer buffer = new StringBuffer();

char[] array = nametoCharArray();

List<Integer> insertIndexes = new ArrayList<>();

for(int i=0;i<arraylength;i++){

Character c = array[i];

if(i != 0 && CharacterisUpperCase(c)){

insertIndexesadd(i);

}

}

if(insertIndexessize() > 0){

int flag = 0;

for(int j=0;j<insertIndexessize();j++){

String word = toLowercase4FirstLetter(namesubstring(flag, insertIndexesget(j)));

bufferappend(word)append("_");

flag = insertIndexesget(j);

}

String last = toLowercase4FirstLetter(namesubstring(flag));

bufferappend(last);

Systemoutprintln(buffertoString());

} else {

Systemoutprintln(name);

}

}

}

}

private String toLowercase4FirstLetter(String word){

if(word != null && wordlength() > 0){

String firstLetter = wordsubstring(0,1);

String others = wordsubstring(1);

return firstLettertoLowerCase() + others;

}else{

return "";

}

}

/

  data base column-key to java field

 /

public void convertToJava(String dbKeys) {

if(dbKeys != null && dbKeyslength > 0){

for(String key : dbKeys){

String[] words = keysplit("_");

String result = toUppercase4FirstLetter(words);

Systemoutprintln(result);

}

}

}

private String toUppercase4FirstLetter(String words){

StringBuffer buffer = new StringBuffer();

if(words != null && wordslength > 0){

for(int i=0;i<wordslength;i++){

String word = words[i];

String firstLetter = wordsubstring(0, 1);

String others = wordsubstring(1);

String upperLetter = null;

if(i != 0){

upperLetter = firstLettertoUpperCase();

} else {

upperLetter = firstLetter;

}

bufferappend(upperLetter)append(others);

}

return buffertoString();

}

return "";

}

}

字符串是Java中的类型,smallmoney是sqlserver中的数据类型,怎么转换你用java把'314'这个字符串变成314,就是float类型,在插入到一个smallmoney字段,就行了

利用request对象获取RequestDispatcher来进行跳转

requestgetRequestDispatcher("路径")forward(request,response);

或者重定向

ResponsesendRedirector(url);

1提取单条记录

//importjavasql;

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

ClassforName("sunjdbcodbc");

Stringurl="jdbc:odbc:%%1";

con=(url,%%2,%%3);

stmt=conn();

stmt(%%4);

rs=stmt(%%5);

}catch(Exceptione){

e();

}

finally{

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(e){

e();

}

}

3显示表格

/

importjavaawt;

importjavaxswing;

importjavasql;

importjavaxswingtable;

String[]colHeads=%%4;

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

ClassforName("sunjdbcodbc");

Stringurl="jdbc:odbc:%%1";

conn=(url,%%2,%%3);

stmt=conn();

rs=stmt("SELECTcount()asau_countfrom"%%5);

rsnext();

intiCount=rsgetInt("au_count");

Object[][]data=newObject[iCount][];

inti=0;

rs=stmt("SELECTfrom"%%5);

while(rsnext()){

data[i]=newObject[iCount];

data[i][0]=rsgetString("au_fname");

data[i][1]=rsgetString("Phone");

data[i][2]=rsgetString("City");

i;

}

JTabletable=newJTable(data,colHeads);

JScrollPanejsp=newJScrollPane(table);

()add(jsp);

}catch(Exceptione){

eprintStackTrace();

}

finally{

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

eprintStackTrace();

}

}

6关闭时关闭连接

//importjavasql;

addWindowListener(newWindowAdapter{

publicvoidwindowClosing(WindowEventwevent){

if(stmt!=null){

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

eprintStackTrace();

}

}

7执行命令

//importjavasql;

Connectionconn=null;

PreparedStatementpst=null;

try{

conn=DriverManagergetConnection(url);

pst=connprepareStatement("InsertIntograde(%%1)Values()");

pstsetInt(1,%%2);

//pstsetString(2,%%2);

pstaddBatch();

pstexecuteBatch();

}catch(SQLExceptione){

eprintStackTrace();

}

finally{

try{

if(pst!=null)

pstclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

eprintStackTrace();

}

}

以上就是关于怎么用java实现驼峰命名法与数据库命名法的相互转换全部的内容,包括:怎么用java实现驼峰命名法与数据库命名法的相互转换、java 中如何将字符串转换成sql server数据库smallmoney类型、JAVA 如何跳转到从数据库中取出来的网址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9704734.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存