save.java
来自「一个论坛小程序,虽然很小,但几乎包括了论坛的所有功能」· Java 代码 · 共 96 行
JAVA
96 行
package save1;
import java.io.*;
import java.sql.*;
import java.util.Date;
public class save
{
Connection con;
public save()
{
String classforname="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String serveraddb="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=luntan";
String user="sa";
String pwd="sa";
try
{
Class.forName(classforname);
con=DriverManager.getConnection(serveraddb,user,pwd);
}catch(Exception e)
{e.printStackTrace();}
}
public ResultSet findnum(String name)
{
try
{
Statement stm=con.createStatement();
ResultSet rest=stm.executeQuery("select * from luntan where title='"+name+"'");
return rest;
}catch(Exception e)
{}
return null;
}
public void createfile(String name)
{
try
{
File file=new File(name+".txt");
if(!file.exists())
{
file.createNewFile();
}
}catch(Exception e)
{}
}
public void adddata(String title,String author,String email)throws Exception
{
try
{
Date date=new Date();
int mouth=date.getMonth()+1;
int year=date.getYear()+1900;
int day=date.getDate();
PreparedStatement pstmt=con.prepareStatement("insert into luntan values(?,?,?,?)");
pstmt.setString(1,title);
pstmt.setString(2,author);
pstmt.setString(3,""+year+"-"+mouth+"-"+day);
pstmt.setString(4,email);
pstmt.execute();
}catch(Exception e)
{}
}
public void updatedata(String id,String title,String author,String email)
{
try
{
Date date=new Date();
int mouth=date.getMonth()+1;
int year=date.getYear()+1900;
int day=date.getDate();
String date1=""+year+"-"+mouth+"-"+day;
Statement stm=con.createStatement();
int i=stm.executeUpdate("update luntan set title='"+title+"',author='"+author+"',email='"+email+"',date='"+date1+"' where title='"+id+"'");
stm.close();
}catch(Exception e)
{}
}
public String deletenum(String title)
{
String str="a";
try
{
//String nameq=name;
Statement stm=con.createStatement();
str="abc";
stm.execute("delete from luntan where title='"+title+"'");
stm.close();
return str;
}catch(Exception e)
{}
return str;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?