62,615
社区成员
发帖
与我相关
我的任务
分享
class DataBaseInsert
{
private Connection conn;
private PreparedStatement ps = null;
DataBaseInsert()
{
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "88888";
String url = "jdbc:mysql://127.0.0.1:3306/classicmodels";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, user,
password);
conn.setAutoCommit(false);
}
catch (Exception e)
{
e.printStackTrace();
}
}
void Insert(Controller controller)
{
try
{
ps = conn.prepareStatement("insert into gamer(score) values (?)");
ps.setInt(1, controller.getScore());
ps.executeUpdate();
}
catch (Exception e)
{
e.printStackTrace();
}
}
void Close()
{
// 关闭声明
if (ps != null)
{
try
{
ps.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
// 关闭链接对象
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}