发布于 2016-03-08 06:45:35 | 127 次阅读 | 评论: 0 | 来源: 网友投递
Mysql关系型数据库管理系统
MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。
String driverName="com.mysql.jdbc.Driver";
String connectiionString="jdbc:mysql://10.5.110.239:3306/test?"+"user=root&password=chen&characterEncoding=utf-8";
Connection connection=null;
try {
Class.forName(driverName);//这里是所谓的数据库驱动的加载
connection=(Connection) DriverManager.getConnection(connectiionString);//这里就是建立数据库连接
System.out.println("数据库连接成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
Statement statement=(Statement) dUtil.getConnection().createStatement();
String sql="delete from diary where title="+"'"+title+"'";
int count=statement.executeUpdate(sql);
System.out.println("删除成功");
String sql="insert into diary(title,content,authorname,time) values(?,?,?,now())";
try {
PreparedStatement preparedStatement=(PreparedStatement) dUtil.getConnection().prepareStatement(sql);
String title=diary.getTitle();
String content=diary.getContent();
String authorname=diary.getAuthorName();
preparedStatement.setString(1, title);
preparedStatement.setString(2, content);
preparedStatement.setString(3, authorname);
ResultSet resultSet=statement.executeQuery(sql);
while (resultSet.next()) {
Diary diary=new Diary();
diary.setAuthorName(resultSet.getString("authorname"));
diary.setContent(resultSet.getString("content"));
diary.setTitle(resultSet.getString("title"));
diary.setId(resultSet.getInt("id"));
Date time=resultSet.getDate("time");
public static void closeConnection(ResultSet resultSet,PreparedStatement preparedStatement, Connection connection) throws SQLException {
if (resultSet!=null) resultSet.close();
if (preparedStatement!=null) preparedStatement.close();
if(connection!=null&&connection.isClosed()==false) connection.close();
System.out.println("数据库关闭");
}