如果找到了对您有用的资料,烦请点击右手边的Google广告支持我继续共享知识,谢谢! http://dengpeng.spaces.live.com/

2007年7月29日星期日

Netbeans and MySQL

在 NetBeans 中构建一个简单的 MySQL 客户端

然后,将mysql-connector-java-5.0.6-bin.jar加入到项目的库中。

/*
* Main.java
*
* Created on 2007年7月29日, 下午4:33
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package mysqlsample;

import java.sql.*;

/**
*
* @author Peng
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
Statement stmt;
ResultSet rs;

Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/sunspotdata";
Connection con = DriverManager.getConnection(url,"root","");
stmt=con.createStatement();

stmt.executeUpdate("INSERT INTO `sunspotdata`.`spotdata` (`DataID` ,`SPOTID` ,`Temperature` ,`Light` ,`TimeStamp` )VALUES (NULL , '3', '1', '5', NOW( ))");

rs= stmt.executeQuery("select * from spotdata");

while(rs.next()){
int id = rs.getInt("DataID");
String spotId = rs.getString("SPOTID");
int temp = rs.getInt("Temperature");
int light = rs.getInt("Light");
Timestamp tt = rs.getTimestamp("TimeStamp");
System.out.println("data id is "+ id + ". spot id is "+ spotId + ". temp is "+temp+". light is " + light+". time is "+tt.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}

}

没有评论: