本文章算是一篇入门教程吧,一篇关于java 数据更新简单教程有需要的同学可以参考一下下吧。

 代码如下


package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnectionManager {  
  private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";  
  private static final String DATABASE_URL="jdbc:sqlserver://127.0.0.1:1433;databasename=news";  
  private static final String DATABASE_USER="sa"; 
  private static final String DATABASE_PASSWORD="123456";  //连接catch(Exception e){
e.printStackTrace();   return dbConnection;  //关闭数据库连接的方法
public static void  closeConnection(Connection dbConnection){
try{
if(dbConnection!=null&&(!dbConnection.isClosed())){
dbConnection.close();   }
catch(SQLException sqlEx){
sqlEx.printStackTrace();    //关闭数据集
public static void closeResultSet(ResultSet res){
try{
if(res!=null){
res.close();
res=null;   }
catch(SQLException e){
e.printStackTrace();  }  public static void closeStatement(PreparedStatement pStatement){
try{
if(pStatement!=null){
pStatement.close();
pStatement=null;   }
catch(SQLException e){
e.printStackTrace();  }
}

----------------------------------------数据的查询---------------------------------------

 代码如下


package com;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class NewFirstTitleDB5 {  public static void main(String[] args) {
int id=0;
Connection con=null;
PreparedStatement pStatement=null;
ResultSet resSet=null;
try{    con=ConnectionManager.getConnection();
//查询数据的SQL语句
String strsql="select max(id) from FirstLeveTitle";    
    pStatement=con.prepareStatement(strsql);   
    resSet=pStatement.executeQuery();
if(resSet.next()){
id=resSet.getInt(1);   }
catch(Exception e){
e.printStackTrace();   finally{
ConnectionManager.closeConnection(con);
ConnectionManager.closeResultSet(resSet);
ConnectionManager.closeStatement(pStatement);   
    System.out.println("新的新闻标题的ID为:" (id 1));