資源簡介
一個可二次開發(fā)的簡單的java對mysql的增刪改查的demo。

代碼片段和文件信息
package?com.sam.bean;
import?java.sql.Connection;
import?java.sql.DriverManager;
import?java.sql.PreparedStatement;
import?java.sql.ResultSet;
import?java.sql.SQLException;
import?java.sql.Statement;
public?class?DB?{
public?static?Connection?getConn()?{
Connection?conn?=?null;
try?
{
//Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver“);
//Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver“);
//conn?=?DriverManager.getConnection(“jdbc:sqlserver://localhost:1433;databaseName=lib“?“sa“?“4682137“);
//conn?=?DriverManager.getConnection(“jdbc:microsoft:sqlsever:localhost://1433;DatabaseName=lib“?“sa“?“4682137“);
try?{
Class.forName(“com.mysql.jdbc.Driver“).newInstance();
}?catch?(InstantiationException?e)?{e.printStackTrace();
}?catch?(IllegalAccessException?e)?{e.printStackTrace();}
//mysql數(shù)據(jù)庫的端口默認是3306;characterEncoding的指編碼格式這個一定要加,不然存到數(shù)據(jù)庫的數(shù)據(jù)會亂碼;最后面的兩個參數(shù)是mysql數(shù)據(jù)庫的用戶名和密碼我的是用戶名是root密碼是root連接你自己的時候你改成你自己的就可以了
conn=?DriverManager.getConnection(“jdbc:mysql://localhost:3306/bishe018?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull““root““root“);?
}catch?(ClassNotFoundException?e)?{
e.printStackTrace();
}catch?(SQLException?e)?{
e.printStackTrace();
}
return?conn;
}
public?static?Statement?createStmt(Connection?conn)?{
Statement?stmt?=?null;
try?
{
stmt?=?conn.createStatement();
}?
catch?(SQLException?e)?
{
e.printStackTrace();
}
return?stmt;
}
public?static?ResultSet?executeQuery(Statement?stmt?String?sql)?{
ResultSet?rs?=?null;
try?
{
rs?=?stmt.executeQuery(sql);
}?
catch?(SQLException?e)?
{
e.printStackTrace();
}
return?rs;
}
public?static?ResultSet?executeQuery(PreparedStatement?preparedstmt)
{
ResultSet?rs?=?null;
try?{
rs?=?preparedstmt.executeQuery();
}?catch?(SQLException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
return?rs;
}
public?static?PreparedStatement?prepareStmt(Connection?conn?String?sql)?{
PreparedStatement?pstmt?=?null;
try?{
pstmt?=?(PreparedStatement)?conn.prepareStatement(sql);
}catch?(SQLException?e)?{
e.printStackTrace();
}
return?pstmt;
}
public?static?void?close(Connection?conn)?{
if?(conn?!=?null)?{
try?{
conn.close();
}?catch?(SQLException?e)?{
e.printStackTrace();
}
}
}
public?static?void?close(Statement?stmt)?{
if?(stmt?!=?null)?
{
try?
{
stmt.close();
}?
catch?(SQLException?e)?
{
e.printStackTrace();
}
}
}
public?static?void?close(ResultSet?rs)?
{
if?(rs?!=?null)?
{
try?{
rs.close();
}
catch?(SQLException?e)?
{
e.printStackTrace();
}
}
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-06-16?13:31??bishe018\
?????文件????????1673??2016-06-16?13:32??bishe018\.classpath
?????文件????????1333??2016-06-16?13:35??bishe018\.project
?????目錄???????????0??2016-06-16?13:35??bishe018\.settings\
?????文件?????????522??2016-06-16?13:32??bishe018\.settings\.jsdtscope
?????文件??????????74??2016-06-16?13:35??bishe018\.settings\com.genuitec.eclipse.core.prefs
?????文件?????????364??2016-06-16?13:31??bishe018\.settings\org.eclipse.jdt.core.prefs
?????文件?????????483??2016-06-16?13:32??bishe018\.settings\org.eclipse.wst.common.component
?????文件?????????750??2016-06-16?13:32??bishe018\.settings\org.eclipse.wst.common.project.facet.core.prefs.xm
?????文件?????????509??2016-06-16?13:32??bishe018\.settings\org.eclipse.wst.common.project.facet.core.xm
?????文件??????????49??2016-06-16?13:32??bishe018\.settings\org.eclipse.wst.jsdt.ui.superType.container
?????文件???????????6??2016-06-16?13:32??bishe018\.settings\org.eclipse.wst.jsdt.ui.superType.name
?????目錄???????????0??2016-06-16?14:28??bishe018\WebRoot\
?????目錄???????????0??2016-06-16?13:32??bishe018\WebRoot\me
?????文件??????????39??2016-06-16?13:32??bishe018\WebRoot\me
?????目錄???????????0??2016-06-16?13:32??bishe018\WebRoot\WEB-INF\
?????目錄???????????0??2016-06-16?13:33??bishe018\WebRoot\WEB-INF\classes\
?????目錄???????????0??2016-06-16?13:33??bishe018\WebRoot\WEB-INF\classes\com\
?????目錄???????????0??2016-06-16?13:33??bishe018\WebRoot\WEB-INF\classes\com\sam\
?????目錄???????????0??2016-06-16?14:27??bishe018\WebRoot\WEB-INF\classes\com\sam\bean\
?????文件????????3191??2016-06-16?13:34??bishe018\WebRoot\WEB-INF\classes\com\sam\bean\DB.class
?????文件????????4909??2016-06-16?14:49??bishe018\WebRoot\WEB-INF\classes\com\sam\bean\Plan.class
?????目錄???????????0??2016-06-16?14:27??bishe018\WebRoot\WEB-INF\classes\com\sam\servlet\
?????文件????????4072??2016-06-16?14:56??bishe018\WebRoot\WEB-INF\classes\com\sam\servlet\planservlet.class
?????目錄???????????0??2016-06-16?13:33??bishe018\WebRoot\WEB-INF\lib\
?????文件??????321838??2015-01-10?12:20??bishe018\WebRoot\WEB-INF\lib\jsf-api.jar
?????文件?????1206500??2015-01-10?12:20??bishe018\WebRoot\WEB-INF\lib\jsf-impl.jar
?????文件??????414240??2015-01-10?12:20??bishe018\WebRoot\WEB-INF\lib\jstl-1.2.jar
?????文件??????709922??2016-05-03?11:40??bishe018\WebRoot\WEB-INF\lib\mysql-connector-java-5.1.7-bin.jar
?????文件??????143939??2016-04-19?10:40??bishe018\WebRoot\WEB-INF\lib\servlet-api.jar
?????文件??????537303??2015-01-10?12:20??bishe018\WebRoot\WEB-INF\lib\sqljdbc4.jar
............此處省略31個文件信息
- 上一篇:Java大作業(yè)個人通訊錄
- 下一篇:JAVA企業(yè)網(wǎng)站
評論
共有 條評論