import?java.sql.*;
成都創(chuàng)新互聯(lián)公司 - 移動服務(wù)器托管,四川服務(wù)器租用,成都服務(wù)器租用,四川網(wǎng)通托管,綿陽服務(wù)器托管,德陽服務(wù)器托管,遂寧服務(wù)器托管,綿陽服務(wù)器托管,四川云主機,成都云主機,西南云主機,移動服務(wù)器托管,西南服務(wù)器托管,四川/成都大帶寬,機柜大帶寬、租用·托管,四川老牌IDC服務(wù)商
public?class?Test
{
public?static?void?main(String[]?args)?throws?Exception
{
Class.forName("com.mysql.jdbc.Driver");
//一開始必須填一個已經(jīng)存在的數(shù)據(jù)庫
String?url?=?"jdbc:mysql://localhost:3306/test?useUnicode=truecharacterEncoding=utf-8";????
Connection?conn?=?DriverManager.getConnection(url,?"root",?"123456");
Statement?stat?=?conn.createStatement();
//創(chuàng)建數(shù)據(jù)庫hello
stat.executeUpdate("create?database?hello");
//打開創(chuàng)建的數(shù)據(jù)庫
stat.close();
conn.close();
url?=?"jdbc:mysql://localhost:3306/hello?useUnicode=truecharacterEncoding=utf-8";
conn?=?DriverManager.getConnection(url,?"root",?"123456");
stat?=?conn.createStatement();
//創(chuàng)建表test
stat.executeUpdate("create?table?test(id?int,?name?varchar(80))");
//添加數(shù)據(jù)
stat.executeUpdate("insert?into?test?values(1,?'張三')");
stat.executeUpdate("insert?into?test?values(2,?'李四')");
//查詢數(shù)據(jù)
ResultSet?result?=?stat.executeQuery("select?*?from?test");
while?(result.next())
{
System.out.println(result.getInt("id")?+?"?"?+?result.getString("name"));
}
//關(guān)閉數(shù)據(jù)庫
result.close();
stat.close();
conn.close();
}
}
服務(wù)器端:
package com.lqq.service;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class DealClientRequest implements Runnable
{
private Socket s = null;
private ParserXML p = new ParserXML(new File("students.xml"));
public DealClientRequest(Socket s)
{
this.s = s;
}
@Override
public void run()
{
if(s != null)
{
try
{
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
QueryType qt = (QueryType) ois.readObject();
if(qt.getQueryType() == 1)//單查
{
System.out.println("執(zhí)行單查,查詢的名字= " + qt.getQueryName());
Student stu = p.getStudent(qt.getQueryName());
oos.writeObject(stu);
}else if(qt.getQueryType() == 2)//全查
{
System.out.println("執(zhí)行全查");
ListStudent list = p.getAllStudent();
for(int i = 0; i list.size(); i++)
{
Student stu = list.get(i);
oos.writeObject(stu);
}
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
}
*************************
package com.lqq.service;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MainService
{
private ServerSocket ss;
public void startService() throws IOException
{
ss = new ServerSocket(10086);
while(true)
{
System.out.println("服務(wù)器在10086等待...");
Socket s = ss.accept();
new Thread(new DealClientRequest(s)).start();
System.out.println("啟動處理線程成功");
}
}
}
******************
package com.lqq.service;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import com.lqq.vo.Student;
public class ParserXML
{
private DocumentBuilderFactory bdf;
private DocumentBuilder db;
private Document dom;
public ParserXML(File file)
{
bdf = DocumentBuilderFactory.newInstance();
try
{
db = bdf.newDocumentBuilder();
dom = db.parse(file);
} catch (Exception e)
{
e.printStackTrace();
}
}
public ListStudent getAllStudent()
{
ListStudent stu = new ArrayListStudent();
Element root = dom.getDocumentElement();
NodeList list = root.getElementsByTagName("stu");
for(int i = 0; i list.getLength(); i++)
{
Element e = (Element) list.item(i);
Student st = new Student();
NodeList names = e.getElementsByTagName("name");
if(names.getLength() == 1)
{
Element name = (Element)names.item(0);
Text text = (Text) name.getFirstChild();
st.setStuName(text.getNodeValue());
}
NodeList sexs = e.getElementsByTagName("sex");
if(sexs.getLength() == 1)
{
Element name = (Element)sexs.item(0);
Text text = (Text) name.getFirstChild();
st.setStuGender(text.getNodeValue());
}
NodeList ages = e.getElementsByTagName("age");
if(ages.getLength() == 1)
{
Element name = (Element)ages.item(0);
Text text = (Text) name.getFirstChild();
st.setStuAge(Integer.parseInt(text.getNodeValue()));
}
NodeList classs = e.getElementsByTagName("class");
if(classs.getLength() == 1)
{
Element name = (Element)classs.item(0);
Text text = (Text) name.getFirstChild();
st.setStuClassName(text.getNodeValue());
}
stu.add(st);
}
return stu;
}
public Student getStudent(String stuName)
{
ListStudent list = this.getAllStudent();
for(int i = 0; i list.size(); i++)
{
Student st = list.get(i);
if(st.getStuName().equals(stuName))
return st;
}
return null;
}
}
*************
package com.lqq.service;
import java.io.IOException;
public class Service
{
public static void main(String[] args)
{
MainService ms = new MainService();
try
{
ms.startService();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
*******
package com.lqq.vo;
import java.io.Serializable;
public class QueryType implements Serializable
{
private static final long serialVersionUID = 8713870836629046060L;
/**
* 1 單查
* 2 全查
*/
private byte queryType;
private String queryName;
public byte getQueryType()
{
return queryType;
}
public void setQueryType(byte queryType)
{
this.queryType = queryType;
}
public String getQueryName()
{
return queryName;
}
public void setQueryName(String string)
{
this.queryName = string;
}
}
************
package com.lqq.vo;
import java.io.Serializable;
public class Student implements Serializable
{
private static final long serialVersionUID = -6087251613589160139L;
private String stuName;
private int stuAge;
private String stuGender;
private String stuClassName;
@Override
public String toString()
{
return "姓名: " + stuName + "\t性別: " + stuGender
+ "\t年齡: " + stuAge + "\t班級: " + stuClassName;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuGender()
{
return stuGender;
}
public void setStuGender(String stuGender)
{
this.stuGender = stuGender;
}
public String getStuClassName()
{
return stuClassName;
}
public void setStuClassName(String stuClassName)
{
this.stuClassName = stuClassName;
}
}
*************
?xml version="1.0" encoding="UTF-8"?
stus
stu
name令狐沖/name
age12/age
class華山派/class
sex男/sex
/stu
stu
name東方不敗/name
age22/age
class日月神教/class
sex女/sex
/stu
stu
name岳不群/name
age23/age
class華山派/class
sex妖/sex
/stu
stu
name風清揚/name
age88/age
class華山派/class
sex男/sex
/stu
/stus
客戶端:
package com.lqq.c;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class AllQuery implements Runnable
{
private Socket s;
public AllQuery(QueryType qt)
{
try
{
s = new Socket("localhost", 10086);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(qt);
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void run()
{
if(s != null)
{
try
{
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Student stu = (Student) ois.readObject();
Client c = new Client();
while(stu != null)
{
c.showStudentInfo(stu);
stu = (Student) ois.readObject();
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
}
**************
package com.lqq.c;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class Client
{
public void showStudentInfo(Student stu)
{
if(stu != null)
{
System.out.println(stu);
}
}
public void showMenu()
{
System.out.println("\t\t菜 ?單");
System.out.println("\t1.單 查");
System.out.println("\t2.全 查");
System.out.println("\t0.退 出");
}
public String getUserInput()
{
Scanner scan = new Scanner(System.in);
System.out.println("請輸入:");
return scan.next();
}
public void requestQuery(String userSelect)
{
if(userSelect != null)
{
if("1".equals(userSelect.trim()))
{
QueryType qt = new QueryType();
qt.setQueryType((byte)1);
Scanner san = new Scanner(System.in);
System.out.println("請輸入查詢的學生名字:");
String queryStuName = san.next();
qt.setQueryName(queryStuName);
this.executeQeury(qt);
}else if("2".equals(userSelect.trim()))
{
QueryType qt = new QueryType();
qt.setQueryType((byte)2);
this.executeQeury(qt);
}else if("0".equals(userSelect.trim()))
{
System.exit(0);
}
else
{
System.out.println("輸入有誤");
}
}
}
public void executeQeury(QueryType qt)
{
try
{
Socket s = new Socket("localhost", 10086);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(qt);
System.out.println("請求發(fā)送完畢");
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
if(qt.getQueryType() == 1)
{
Student stu = (Student) ois.readObject();
showStudentInfo(stu);
}else if(qt.getQueryType() == 2)
{
new Thread(new AllQuery(qt)).start();
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Client client = new Client();
while(true)
{
client.showMenu();
client.requestQuery(client.getUserInput());
}
}
}
**********
package com.lqq.vo;
import java.io.Serializable;
public class QueryType implements Serializable
{
private static final long serialVersionUID = 8713870836629046060L;
/**
* 1 單查
* 2 全查
*/
private byte queryType;
private String queryName;
public byte getQueryType()
{
return queryType;
}
public void setQueryType(byte queryType)
{
this.queryType = queryType;
}
public String getQueryName()
{
return queryName;
}
public void setQueryName(String string)
{
this.queryName = string;
}
}
**********
package com.lqq.vo;
import java.io.Serializable;
public class Student implements Serializable
{
private static final long serialVersionUID = -6087251613589160139L;
private String stuName;
private int stuAge;
private String stuGender;
private String stuClassName;
@Override
public String toString()
{
return "姓名: " + stuName + "\t性別: " + stuGender
+ "\t年齡: " + stuAge + "\t班級: " + stuClassName;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuGender()
{
return stuGender;
}
public void setStuGender(String stuGender)
{
this.stuGender = stuGender;
}
public String getStuClassName()
{
return stuClassName;
}
public void setStuClassName(String stuClassName)
{
this.stuClassName = stuClassName;
}
}
時間太緊,沒有完善代碼,基本功能實現(xiàn)了,代碼沒有優(yōu)化,你自己再改改,希望可以幫到你
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class CreateFile {
public static void main(String[] args) {
String str = "需要寫入的字";
String fileName = "D:\\a\\a.xml";
OutputStream output = null;// 輸出字節(jié)流
OutputStreamWriter outputWrite = null;// 輸出字符流
PrintWriter print = null;// 輸出緩沖區(qū)
try {
output = new FileOutputStream(fileName);
outputWrite = new OutputStreamWriter(output);
print = new PrintWriter(outputWrite);
print.print(str);
print.flush();// 一定不要忘記此句,否則數(shù)據(jù)有可能不能被寫入文件
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
再一個問題就是只要你編碼正確就可以正常打開。
Java代碼:
import java.io.File;
import java.io.IOException;
public class Test10 {
public static void main(String[] args) {
//創(chuàng)建“abc”文件夾
String pathName = "d:\\abc";
File path = new File(pathName);
path.mkdir();
//創(chuàng)建“abc”文件
String fileName = "d:\\abc\\abc";
File file = new File(fileName);
try {
file.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
可以的,我說說大概思路,很簡單,你自己具體實現(xiàn)吧,把代碼寫給你沒意義的:
1.將你這段字符串輸出到一個文件里,用Java類文件的方式命名。
2.調(diào)用外部javac命令將該文件編譯。
3.用類加載器(ClassLoad)動態(tài)加載新的class文件并用Class.forName()注冊該類,然后就可以正常使用了。
上面的每一步都能在baidu中找到實現(xiàn)方法,自己發(fā)揮吧。