//第一題:Test.java
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名申請、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、銅陵網(wǎng)站維護、網(wǎng)站推廣。
public class Test {
public static void main(String[] args) {
int array[]=new int[10];//聲明數(shù)組并聲明
array[0]=array[1]=1;//賦初始值
System.out.println(array[0]);
System.out.println(array[1]);
for(int i=2;iarray.length;i++){
array[i]=array[i-2]+array[i-1];//用for語句完成相應(yīng)的運算
System.out.println(array[i]);
}
}
}
//第二題 RandomTest.java
import java.util.Random;//隨機數(shù)包聲明
import java.util.Scanner;//用戶輸入包聲明
public class RandomTest {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);//聲明并創(chuàng)建用戶輸入對象
System.out.println("我心里有一個0~99之間的數(shù),你猜是什么?");
int i=(int)(Math.random()*100);//生成一個隨機數(shù)
int input=reader.nextInt();//用戶輸入
while(i!=input){//判斷用戶輸入的數(shù)和隨機生數(shù)的大小
if(inputi){
System.out.println("小了點,再猜!");
}
if(inputi){
System.out.println("大了點,再猜");
}
input=reader.nextInt();
}
System.out.println("猜對了!");
System.out.println("繼續(xù)加油!");
}
}
你好,代碼如下。需要修改的話,你可以根據(jù)情況修改:
class Info{ // 定義信息類
private String name = "生產(chǎn)者"; // 定義name屬性
private String content = "壓入子彈" ; // 定義content屬性
private boolean flag = false ; // 設(shè)置標志位
public synchronized void set(String name,String content){
if(!flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
this.setName(name) ; // 設(shè)置名稱
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
this.setContent(content) ; // 設(shè)置內(nèi)容
flag = false ; // 改變標志位,表示可以取走
super.notify() ;
}
public synchronized void get(){
if(flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
System.out.println(this.getName() +
" -- " + this.getContent()) ;
flag = true ; // 改變標志位,表示可以生產(chǎn)
super.notify() ;
}
public void setName(String name){
this.name = name ;
}
public void setContent(String content){
this.content = content ;
}
public String getName(){
return this.name ;
}
public String getContent(){
return this.content ;
}
};
class Producer implements Runnable{ // 通過Runnable實現(xiàn)多線程
private Info info = null ; // 保存Info引用
public Producer(Info info){
this.info = info ;
}
public void run(){
boolean flag = false ; // 定義標記位
for(int i=0;i12;i++){
if(flag){
this.info.set("生產(chǎn)者","壓入子彈") ; // 設(shè)置名稱
flag = false ;
}else{
this.info.set("消費者","射出子彈") ; // 設(shè)置名稱
flag = true ;
}
}
}
};
class Consumer implements Runnable{
private Info info = null ;
public Consumer(Info info){
this.info = info ;
}
public void run(){
for(int i=0;i24;i++){
this.info.get() ;
}
}
};
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info = new Info(); // 實例化Info對象
Producer pro = new Producer(info) ; // 生產(chǎn)者
Consumer con = new Consumer(info) ; // 消費者
new Thread(pro).start() ;
new Thread(con).start() ;
}
};
1.Java連接MySQL數(shù)據(jù)庫
Java連接MySql需要下載JDBC驅(qū)動MySQL-connector-java-5.0.5.zip(舉例,現(xiàn)有新版本)。然后將其解壓縮到任一目錄。我是解壓到D盤,然后將其目錄下的MySQL-connector-java-5.0.5-bin.jar加到classpath里,具體如下:
“我的電腦”- “屬性” - “高級” - “環(huán)境變量”,在系統(tǒng)變量那里編輯classpath,將D:\MySQL-connector-java-5.0.5\MySQL-connector-java-5.0.5-bin.jar加到最后,在加這個字符串前要加“;”,以與前一個classpath區(qū)分開。然后確定。
package hqs;
import java.sql.*;
public class DataBasePractice {
public static void main(String[] args) {
//聲明Connection對象
Connection con;
//驅(qū)動程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要訪問的數(shù)據(jù)庫名mydata
String url = "jdbc:mysql://localhost:3306/mydata";
//MySQL配置時的用戶名
String user = "root";
//MySQL配置時的密碼
String password = "root";
//遍歷查詢結(jié)果集
try {
//加載驅(qū)動程序
Class.forName(driver);
//1.getConnection()方法,連接MySQL數(shù)據(jù)庫?。?/p>
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.創(chuàng)建statement類對象,用來執(zhí)行SQL語句!!
Statement statement = con.createStatement();
//要執(zhí)行的SQL語句
String sql = "select * from student";
//3.ResultSet類,用來存放獲取的結(jié)果集??!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("執(zhí)行結(jié)果如下所示:");
System.out.println("-----------------");
System.out.println(" 學號" + "\t" + " 姓名");
System.out.println("-----------------");
String name = null;
String id = null;
while(rs.next()){
//獲取stuname這列數(shù)據(jù)
name = rs.getString("stuname");
//獲取stuid這列數(shù)據(jù)
id = rs.getString("stuid");
//首先使用ISO-8859-1字符集將name解碼為字節(jié)序列并將結(jié)果存儲新的字節(jié)數(shù)組中。
//然后使用GB2312字符集解碼指定的字節(jié)數(shù)組。
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
//輸出結(jié)果
System.out.println(id + "\t" + name);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//數(shù)據(jù)庫驅(qū)動類異常處理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//數(shù)據(jù)庫連接失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("數(shù)據(jù)庫數(shù)據(jù)成功獲?。?!");
}
}
}
2.添加、修改、刪除操作
在上面while代碼段后面添加以下代碼段:String name = null;
String id = null;
while(rs.next()){
//獲取stuname這列數(shù)據(jù)
name = rs.getString("stuname");
//獲取stuid這列數(shù)據(jù)
id = rs.getString("stuid");
//首先使用ISO-8859-1字符集將name解碼為字節(jié)序列并將結(jié)果存儲新的字節(jié)數(shù)組中。
//然后使用GB2312字符集解碼指定的字節(jié)數(shù)組。
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
//輸出結(jié)果
System.out.println(id + "\t" + name);
}
PreparedStatement psql;
ResultSet res;
//預處理添加數(shù)據(jù),其中有兩個參數(shù)--“?”
psql = con.prepareStatement("insert into student values(?,?)");
psql.setInt(1, 8); //設(shè)置參數(shù)1,創(chuàng)建id為5的數(shù)據(jù)
psql.setString(2, "xiaogang"); //設(shè)置參數(shù)2,name 為小明
psql.executeUpdate(); //執(zhí)行更新
//預處理更新(修改)數(shù)據(jù)
psql = con.prepareStatement("update student set stuname = ? where stuid = ?");
psql.setString(1,"xiaowang"); //設(shè)置參數(shù)1,將name改為王五
psql.setInt(2,10); //設(shè)置參數(shù)2,將id為2的數(shù)據(jù)做修改
psql.executeUpdate();
//預處理刪除數(shù)據(jù)
psql = con.prepareStatement("delete from student where stuid = ?");
psql.setInt(1, 5);
psql.executeUpdate();
//查詢修改數(shù)據(jù)后student表中的數(shù)據(jù)
psql = con.prepareStatement("select*from student");
res = psql.executeQuery(); //執(zhí)行預處理sql語句
System.out.println("執(zhí)行增加、修改、刪除后的數(shù)據(jù)");
while(res.next()){
name = res.getString("stuname");
id = res.getString("stuid");
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
System.out.println(id + "\t" + name);
}
res.close();
psql.close();
該代碼段使用到了預處理語句:con.prepareStatement(String sql);
這樣生成數(shù)據(jù)庫底層的內(nèi)部命令,并將該命令封裝在preparedStatement對象中,可以減輕數(shù)據(jù)庫負擔,提高訪問數(shù)據(jù)庫速度。 運行結(jié)果:
本文題目:java在線試卷代碼 java在線考試題庫
轉(zhuǎn)載來于:http://muchs.cn/article8/ddehgop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、營銷型網(wǎng)站建設(shè)、網(wǎng)站維護、網(wǎng)站改版、小程序開發(fā)、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)