XML连接数据库
Sql.XML
<?xml version="1.0" encoding="gb2312"?> <DATA> <User> <IP>localhost</IP> <Port>1433</Port> <DBName>mvc</DBName> <UserName>sa</UserName> <PassWord></PassWord> </User> <DataBase type="on"> <ClassForName>com.microsoft.jdbc.sqlserver.SQLServerDriver</ClassForName> <URL>jdbc:microsoft:sqlserver://</URL> <Temp>:</Temp> <DataBaseName>;DatabaseName=</DataBaseName> </DataBase> <!-- <User> <IP>localhost</IP> <Port>3306</Port> <DBName>poi_mvc</DBName> <UserName>root</UserName> <PassWord>123456</PassWord> </User> <DataBase type="on"> <ClassForName>sql.Driver</ClassForName> <URL>jdbc:mysql://</URL> <Temp>:</Temp> <DataBaseName>/</DataBaseName> </DataBase> --> </DATA> |
java连接sqlserver数据库XML_SQL_BEAN.java(与数据库建立连接)
package SQLBean; l.parsers.*; import org.w3c.dom.*; import java.sql.*; public class XML_SQL_BEAN { String url; String cfn; String ip; String port; String dbn; String dbname; String use; String pass; String temp; Connection con=null; Statement sta=null; public XML_SQL_BEAN() throws Exception { DocumentBuilderFactory factory = wInstance(); DocumentBuilder builder = wDocumentBuilder(); Document doc = builder.parse("l"); NodeList nl = ElementsByTagName("User"); for (int i = 0; i < nl.getLength(); i++) { Element node = (Element) nl.item(i); ip = ElementsByTagName("IP").item(0).getFirstChild() .getNodeValue(); port = ElementsByTagName("Port").item(0).getFirstChild().getNodeValue(); dbname = ElementsByTagName("DBName").item(0).getFirstChild().getNodeValue(); use = ElementsByTagName("UserName").item(0).getFirstChild() .getNodeValue(); if (ElementsByTagName("PassWord").item(0).getFirstChild() == null) { pass = ""; } else { pass = ElementsByTagName("PassWord").item(0) .getFirstChild().getNodeValue(); break; } } nl = ElementsByTagName("DataBase"); for (int i = 0; i < nl.getLength(); i++) { Element node = (Element) nl.item(i); if (!"on".Attribute("type"))) continue; cfn = ElementsByTagName("ClassForName").item(0) .getFirstChild().getNodeValue(); url = ElementsByTagName("URL").item(0).getFirstChild() .getNodeValue(); temp = ElementsByTagName("Temp").item(0).getFirstChild() .getNodeValue(); dbn = ElementsByTagName("DataBaseName").item(0) .getFirstChild().getNodeValue(); break; } Class.forName(cfn); String str = null; str = (url + ip + temp + port + dbn + dbname); con = Connection(str, use, pass); sta = ateStatement(); } Statement getStatement() { return sta; } } |
XML_SQL_Change.java(增,删,改功能)
package SQLBean; import java.sql.*; public class XML_SQL_Change { Statement sta=null; public XML_SQL_Change()throws Exception { sta=new XML_SQL_BEAN().getStatement(); } public void updata(String sql)throws Exception { uteUpdate(sql); } } |
XML_SQL_ResultSet.java(查询功能)
package SQLBean; import java.sql.*; public class XML_SQL_ResultSet { Statement sta=null; public XML_SQL_ResultSet()throws Exception { sta=new XML_SQL_BEAN().getStatement(); } public ResultSet select(String select)throws Exception { uteQuery(select); } } |
TrySQL.java(测试连接)
import SQLBean.XML_SQL_BEAN; import SQLBean.XML_SQL_ResultSet; import java.sql.*; public class trySQL { public static void main(String[] args) { try { XML_SQL_ResultSet sql=new XML_SQL_ResultSet(); ResultSet res=sql.select("select * from student"); ()) { System.out.String(2)); } } catch (Exception e) { e.printStackTrace(); } } } |
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论