2,安装
3,启动一个cmd窗口,敲入javac 如果显示不是命令的话,把环境变量配置一下。
计算机⇒属性⇒系统保护⇒高级⇒环境变量⇒系统变量⇒path
假如的你的jdk安装目录为E:\jdk就把E:\jdk\bin这个地址复制到path里面去,注意后面加个分号()
4,再次运行javac 会显示一些内容,恭喜你搭建好了。
以下是Sun提供的一个简单的“Knock Knock"”服务器:import java.net.*import java.io.*public class KnockKnockServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null
try {
serverSocket = new ServerSocket(4444)
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.")
System.exit(1)
}
Socket clientSocket = null
try {
clientSocket = serverSocket.accept()
} catch (IOException e) {
System.err.println("Accept failed.")
System.exit(1)
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()))
String inputLine, outputLine
KnockKnockProtocol kkp = new KnockKnockProtocol()
outputLine = kkp.processInput(null)
out.println(outputLine)
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine)
out.println(outputLine)
if (outputLine.equals("Bye."))
break
}
out.close()
in.close()
clientSocket.close()
serverSocket.close()
}}
再简单不过了。
java+mysql搭建服务器的步骤如下:
1、在工程项目中导入mysql-connection-java-5.0.16-bin.jar这个jar包;
2、编写一个数据库管理类(MyDBHelper)用来配置数据库的属性:
package com.hu.demoimport java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.SQLException
public class MyDBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/student"
public static final String name = "com.mysql.jdbc.Driver"
public static final String user = "root"
public static final String password = "root"
public Connection conn = null
public PreparedStatement pst = null
public DBHelper(String sql) {
try {
Class.forName(name)//指定连接类型
conn = DriverManager.getConnection(url, user, password)//获取连接
pst = conn.prepareStatement(sql)//准备执行语句
} catch (Exception e) {
e.printStackTrace()
}
}
public void close() {
try {
this.conn.close()
this.pst.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
}
3、编写一个测试类(Test)来测试相关数据库操作:
package com.hu.demoimport java.sql.ResultSet
import java.sql.SQLException
public class Test{
static String sql = null
static DBHelper db1 = null
static ResultSet ret = null
public static void main(String[] args) {
sql = "select *from stuinfo"//SQL语句
db1 = new DBHelper(sql)//创建DBHelper对象
try {
ret = db1.pst.executeQuery()//执行语句,得到结果集
while (ret.next()) {
String uid = ret.getString(1)
String ufname = ret.getString(2)
String ulname = ret.getString(3)
String udate = ret.getString(4)
System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate )
}//显示数据
ret.close()
db1.close()//关闭连接
} catch (SQLException e) {
e.printStackTrace()
}
}
}
4、至此就完全了java+mysql服务器的搭建。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)