liangoogle

liangoogle
随笔 - 9, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

jsp使用servlet在xml处添加的信息q.html为这个servlet类的链接地址

<servlet>
<servlet-name>sda</servlet-name>
<servlet-class>com.servlet.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sda</servlet-name>
<url-pattern>/q.html</url-pattern>
</servlet-mapping>

posted @ 2011-04-26 01:25 haojinlian 阅读(126) | 评论 (0)编辑 收藏

java代码实现连接mysql数据库

注意修改下面数据库的名称、登陆的账号密码。
我用的数据库名是:test
账号密码都是:root
=================================
import java.sql.*;
public class JDBCTest {
public static void main(String[] args) {
// 1. 注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
}
// 声明变量,使用,而后关闭
Connection conn = null;        //数据库连接
Statement stmt = null;         //数据库表达式
ResultSet rs = null;             //结果集
try {
//2. 获取数据库的连接
conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/test","root","root");
//3. 获取表达式
stmt = conn.createStatement();
//4. 执行SQL
String sql = "select time from shijian where id=123";
rs = stmt.executeQuery(sql);
//5. 现实结果集里面的数据
while(rs.next()) {
System.out.println("id为123的time值=" + rs.getString(1));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
if(rs != null) {
rs.close();
}
if(stmt!= null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}

posted @ 2011-04-26 00:38 haojinlian 阅读(132) | 评论 (0)编辑 收藏

URL对象的创建及使用 URL类中一些很基本的方法

URL类中一些很基本的方法如下:

·  public final Obect getContent() 这个方法取得传输协议。

·  public String getFile() 这个方法取得资源的文件名。

·  public String getHost() 这个方法取得机器的名称。

·  public int getPort() 这个方法取得端口号。

·  public String getProtocol() 这个方法取得传输协议。

·  public String toString() 这个方法把URL转化为字符串。

实例:URL对象的创建及使用

import java.net. *;
import java.io.*;
class Myurl  {
public static void main(String args[])   {
   try {
    URL url=new URL("http://www.tsinghua.edu.cn/chn/index.htm");
    System.out.println("the Protocol: "+url.getProtocol());
    System.out.println("the hostname: " +url.getHost());
    System.out.println("the port: "+url.getPort());
    System.out.println("the file:"+url.getFile());
System.out.println(url.toString());
}catch(MalformedURLException e) {
System.out.println(e);
     }
    }

}

posted @ 2011-04-21 19:50 haojinlian 阅读(1057) | 评论 (0)编辑 收藏

取得主机名和ip地址字符串

import java.net.*;
import java.io.*;
class host {
public static void main(String args[]) {
try {
InetAddress inetadd;
inetadd=InetAddress.getLocalHost();
System. out. println("hostname="+inetadd.getHostName());//取得主机名
System. out. println(inetadd.toString() );//取得主机名和ip地址字符串
}
catch(Exception e) {
System.out.println(e);
}
}
}

posted @ 2011-04-21 19:27 haojinlian 阅读(176) | 评论 (0)编辑 收藏

仅列出标题
共3页: 上一页 1 2 3 下一页