复制代码 代码如下:
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 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 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("数据库关闭");
}
新闻热点
疑难解答