mysql数据库修改添加Date格式列的方法
importjava.sql.*;
importjava.text.DateFormat;
//数据库的查询
publicclassSelectTable{
StringdbDriver="com.mysql.jdbc.Driver";
StringdbUrl="jdbc:mysql://localhost:3306/sss";//根据实际情况变化
Stringusername="root";
Stringpassword="123";
publicConnectiongetConn()
{
Connectionconn=null;
try
{
Class.forName(dbDriver);
}
catch(ClassNotFoundExceptione)
{
e.printStackTrace();
}
try
{
conn=DriverManager.getConnection(dbUrl,username,password);//注意是三个参数
}
catch(SQLExceptione)
{
e.printStackTrace();
}
returnconn;
}
publicvoidselect(){
Connectionconn=getConn();
try{
Statementstmt=conn.createStatement();//创建Statement对象
System.out.println("成功连接到数据库!");
Stringsql="select*fromjdbc";//要执行的SQL
ResultSetrs=stmt.executeQuery(sql);//创建数据对象
System.out.println("id"+"\t"+"name"+"\t"+"brithday");
while(rs.next()){
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getDate(3)+"\t");
System.out.println();
}
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidinsert(){
Connectionconn=getConn();
try{
Statementstmt=conn.createStatement();
System.out.println("成功连接到数据库!");
Stringsql="insertintojdbc(id,name,birthday)values(?,?,?)";
PreparedStatementpst=conn.prepareStatement(sql);
DateFormatdf=DateFormat.getDateInstance();
java.util.Datedd=df.parse("2000-12-12");//将YYYY-MM-DD格式的时间转换为date
longt=dd.getTime();
java.sql.Datedate=newjava.sql.Date(t);
pst.setInt(1,5);
pst.setString(2,"limazhi");
pst.setDate(3,date);
pst.executeUpdate();
select();
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(Stringargs[]){
SelectTablest=newSelectTable();
st.insert();
}
}热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短