本帖最后由 zhangbingyuan 于 2014-4-6 20:48 编辑
数据查询:package simple; import java.sql.*; public class T{
public static void main(String[] args) {
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//SQL数据库引擎
String connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Mydb";
//数据源
try { Class.forName(JDriver);
//加载数据库引擎,返回给定字符串名的类
}catch(ClassNotFoundException e) {
e.printStackTrace();
System.out.println("加载数据库引擎失败");
System.exit(0);
}
System.out.println("数据库驱动成功");
try {
char id = '2';
String user="sa";
String password="sa";
Connection con=DriverManager.getConnection(connectDB,user,password);
//连接数据库对象
System.out.println("连接数据库成功");
Statement stmt=con.createStatement();
//创建SQL命令对象 /*
//创建表 System.out.println("开始创建表");
String query="create table TABLE1(ID NCHAR(2),NAME NCHAR(10))";
//创建表SQL语句
stmt.executeUpdate(query);
//执行SQL命令对象
System.out.println("表创建成功");
//输入数据 System.out.println("开始插入数据");
String a1="INSERT INTO TABLE1 VALUES('1','旭哥')";
//插入数据SQL语句
String a2="INSERT INTO TABLE1 VALUES('2','伟哥')";
String a3="INSERT INTO TABLE1 VALUES('3','张哥')";
stmt.executeUpdate(a1);//执行SQL命令对象 stmt.executeUpdate(a2);
stmt.executeUpdate(a3); System.out.println("插入数据成功"); */
//读取数据 System.out.println("开始读取数据");
ResultSet rs=stmt.executeQuery("SELECT * FROM TABLE1 WHERE ID=id");
//返回SQL语句查询结果集(集合)
//循环输出每一条记录
while(rs.next())
{
//输出每个字段
System.out.println(rs.getString("ID")+"\t"+rs.getString("NAME"));
}
System.out.println("读取完毕");
//关闭连接
stmt.close();
//关闭命令对象连接
con.close();
//关闭数据库连接
} catch(SQLException e) {
e.printStackTrace();
//System.out.println("数据库连接错误");
System.exit(0);
}
}
}
数据更新:
import java.sql.*;
public class Example {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null; Statement stmt = null; try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "UPDATE Registration " + "SET age = 30 WHERE id in (100, 101)";
stmt.executeUpdate(sql);
// Now you can extract all the records
// to see the updated records
sql = "SELECT id, first, last, age FROM Registration";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close(); }catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close(); }catch(SQLException se){
}
// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace(); }
//end finally try }
//end try
System.out.println("Goodbye!"); }
//end main
}
|