📄 newsdao.java
字号:
package lee;
import java.sql.*;
/*
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class NewsDao
{
public void saveNews(News news)
{
Connection conn = null;
PreparedStatement pstmt = null;
int newsId = news.getId();
String title = news.getTitle();
String content = news.getContent();
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/j2ee?user=root&password=pass";//j2ee为你想连接的数据库
String user="scott";
String password="tiger";
conn= DriverManager.getConnection(url);
pstmt=conn.prepareStatement("insert into news_table values(?,?,?)");
pstmt.setInt(1,newsId);
pstmt.setString(2,title);
pstmt.setString(3,content);
pstmt.executeUpdate();
}
catch (ClassNotFoundException cnf)
{
cnf.printStackTrace();
}
catch (SQLException se)
{
se.printStackTrace();
}
finally
{
try
{
if (pstmt != null)pstmt.close();
if (conn != null) conn.close();
}
catch (SQLException se2)
{
se2.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -