jdbctest.java

来自「里面所含源码是本人平时做程序的一些实例」· Java 代码 · 共 59 行

JAVA
59
字号
import java.sql.*;
import demo.NoticeBean;
public class JdbcTest {
	public static void main(String[] args) {
		Connection conn=null;
		Statement st=null;
		ResultSet rs=null;
		try{
			String dirvername="org.gjt.mm.mysql.Driver";
			Class.forName(dirvername);
			String url="jdbc:mysql://localhost:3306/myproject?useUnicode=true&character Encoding=gb2312";
			conn=DriverManager.getConnection(url,"root","102386");
			System.out.println("连接数据库成功!");
			String title="xue";
			String fileRecord="中国";
			String content=new String(fileRecord.getBytes("gb2312"),"ISO-8859-1");   
			
			String strSql="insert into notice(title,content) values('"+title+"','"+content+"')";
			st=conn.createStatement();
			st.executeUpdate(strSql);
			System.out.println("插入成功!");
			String strSql2="select * from notice";
			rs=st.executeQuery(strSql2);
			while(rs.next()){
				System.out.println("ID="+rs.getInt("id"));
				System.out.println("title="+rs.getString("title"));
				fileRecord=rs.getString("content");
				System.out.println("content="+new String(fileRecord.getBytes("ISO-8859-1"),"gb2312"));
			}
			String strSql3="delete from notice";
			//st.executeUpdate(strSql3);
			//System.out.println("删除成功!");	
		}
		
		catch(Exception e)
		{
			System.out.print(e.getStackTrace());
		}
		finally{
			try{
				if(rs!=null){
					rs.close();
					rs=null;
				}
				if(st!=null){
					st.close();
					st=null;
				}
				if(conn!=null){
					conn.close();
					conn=null;
				}
			}catch(Exception e){}
		}

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?