JDBC,JDBC连接数据库

package servlet_jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class News_JDBC {
 private static Connection conn = null;
 private static Statement stmt = null;
 private static ResultSet rs = null;

 public static Connection getConnect()
 {
    Connection conn1 = null;
    try {
     Class.forName("com.mysql.jdbc.Driver");
     conn1=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ftp20100620","root","111111");
     if(conn1 == null)
     {
      System.out.println("AA");
     }
    } catch (Exception e) {
     System.out.println("conn bad!");
     e.printStackTrace();
    }
    return conn1;
 }
 
 /**
  * 查看所有
  * @throws SQLException
  *
  */
 
 public static ResultSet getResult(String sql) throws SQLException{
  stmt=News_JDBC.getConnect().createStatement();
  try {
   rs=stmt.executeQuery(sql);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return rs;
 }
 
 
 /**
  * 添加
  * @throws SQLException
  *
  */
 
 public static void save(String sql) throws SQLException{
  stmt=News_JDBC.getConnect().createStatement();
   try {
    stmt.execute(sql);
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 }
 
 /**
  * 删除
  * @param rs
  * @throws SQLException
  */
 public static void delete(String sql) throws SQLException{
  stmt=News_JDBC.getConnect().createStatement();
  try {
   stmt.executeUpdate(sql);
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 
 /**
  * 修改
  * @param rs
  * @throws SQLException
  */
 public static void modify(String sql) throws SQLException{
  stmt=News_JDBC.getConnect().createStatement();
  try {
   stmt.executeUpdate(sql);
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 
 /**
  * 通过id查看
  * @param rs
  * @throws SQLException
  */
 public static ResultSet getResultById(String sql,int id) throws SQLException{
  stmt=News_JDBC.getConnect().createStatement();
  try {
   rs=stmt.executeQuery(sql + " where id='"+id+"' ");
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return rs;
 }
 
 /**
  * 通过ID查看新闻名字
  * @return
  * @throws SQLException
  */
 public static String getNewsTypeNameBiId(String sql) throws SQLException{
  String result="";
  stmt=News_JDBC.getConnect().createStatement();
  rs=stmt.executeQuery(sql);
     while (rs.next()) {
            result = rs.getString(1);
        }
     return result;
 }
 
 
 /**
  * 关闭结果集
  * @param rs
  */
 public static void rsClose(ResultSet rs){
  try {
   rs.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * 关闭语句对象
  * @param rs
  */
 public static void stmtClose(Statement stmt){
  try {
   stmt.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * 关闭连接
  * @param rs
  */
 public static void connClose(Connection conn){
  try {
   conn.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值