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();
}
}
}