A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© fightingwang 中级黑马   /  2015-1-27 00:43  /  770 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1、线程创建和操作
(1)class Ticket implements Runnable
{ private  int tick = 100;
public void run(){
while(true){
if(tick>0){
System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);}}}}
class  TicketDemo{
public static void main(String[] args){
Ticket t = new Ticket();
Thread t1 = new Thread(t);//创建一个线程;
     ···
Thread t4 = new Thread(t);//创建一个线程;
                t1.start();···t4.start();}}
(2)class Demo extends Thread
{public void run(){
for(int x=0; x<60; x++)
        System.out.println("demo run----"+x);}}
class ThreadDemo
{public static void main(String[] args) {
        Demo d = new Demo(); d.run();
        for(int x=0; x<60; x++)
        System.out.println("Hello World!--"+x);}}
2、Scocet服务器和客户机的交互
import java.io.*;import java.net.*;
class  TcpClient{
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.1.254",10003);
OutputStream out = s.getOutputStream();
        out.write("我是客户端".getBytes());
        s.close();}}
class  TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10003);
                while(true){
                        Socket s = ss.accept();
        InputStream in = s.getInputStream();
                        byte[] buf = new byte[1024];
                        int len = in.read(buf);
System.out.println(new String(buf,0,len));
                        s.close();
}}}
3、JDBC数据库连接进行查询和修改工作
public class test {
        public static void main(String[] args) {
String dbURL="jdbc:odbc:mydb";//数据库标识名
String user="lizhifang";//数据库用户
String password="lizhifang";//数据库用户密码
        try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//装载数据库驱动
Connection con = DriverManager.getConnection(dbURL, user, password ); //得到连接
Statement st=con.createStatement();//新建表
System.out.println("新建表:mytable");//输出信息到控制台
//新建表的SQL语句
String sqlStr="create table mytable(num int,name varchar(20),sex varchar(2),age int,score double)";
st.executeUpdate(sqlStr); //执行SQL语句,新建表
sqlStr="insert into mytable(num,name,sex,age,score) values(01,\'李\',\'男\',20,78.0)"; //插入数据SQL语句
st.executeUpdate(sqlStr); //执行插入
//显示数据
sqlStr="select * from mytable where score>80.0"; //查询数据SQL语句
ResultSet rs=st.executeQuery(sqlStr); //获取结果集
int num ;        String name ;String sex ;int age ; double score ;
while (rs.next()){        num=rs.getInt("num");
                        name=rs.getString("name"); //取得查询结果
                        sex=rs.getString("sex");
                        age=rs.getInt("age");
                        score=rs.getDouble("score");}
sqlStr="update mytable set score=60.0,age=age+1 where score<60.0"; //查询数据SQL语句
st.executeUpdate(sqlStr); //获取结果集
con.close(); //关闭连接        }
catch (Exception ex){
ex.printStackTrace();  //输出出错信息        }}}
4、I/O流:
public class test1 {
        public static void main(String[] args) {
                try{                FileInputStream in = new FileInputStream("D:\\readme.txt");
                    FileOutputStream out = new FileOutputStream("D:\\readme1.txt");int a = -1;
while((a=in.read()) !=-1){out.write(a);}
} catch (Exception e){e.printStackTrace();}
try {FileInputStream in = new FileInputStream("D:\\readme1.txt");
byte[] buff = new byte[1024];
                        int len = 0;
                        while ((len = in.read(buff))!=-1) {
                                System.out.println(new String(buff, 0, len));}
                } catch (Exception e) {
                        e.printStackTrace();
                }}}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马