jdbc api链接数据库

最典型的链接语句,写这里,方便查找
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;

public class TestDate {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null; 
		
		String url = new String("jdbc:mysql://localhost:3306/BLOG_MUTIL_FUNCTION");
		String sql = new String("select M_PUBLISH_TIME from Message where ID =1");
		
		try
		{ 
			Class.forName("com.mysql.jdbc.Driver"); 
			conn = DriverManager.getConnection(url, "root", "mysql" );
			stmt = conn.createStatement();
			rs=stmt.executeQuery(sql);
			
			String dateStr = null;
			if(rs.next())
			{
				dateStr = rs.getString("M_PUBLISH_TIME");
				
			}
			
			
			dateStr = dateStr.substring(0, dateStr.lastIndexOf("."));
			
			System.out.println(dateStr);
		} 
		catch(ClassNotFoundException e)
		{ 
			e.printStackTrace(); 
		} catch (SQLException se) {
			// TODO Auto-generated catch block
			se.printStackTrace();
		}
		
		
	}

}

你可能感兴趣的:(java,sql,mysql,jdbc,Blog)