java 链接jdbc时设置链接数据库编码

import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import jxl.Sheet;
import jxl.Workbook;

public class ExcelUpload {

 Connection conn = null;

 public Connection getConnection() {

  try {

   Class.forName("com.mysql.jdbc.Driver");

   String url = "jdbc:mysql://localhost:3306/exceltest1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";
   String username = "root";
   String password = "123456";

   conn = DriverManager.getConnection(url, username, password);
  } catch (Exception e) {
   e.printStackTrace();
  }

  return conn;
 }

 public void doIt() {
  PreparedStatement stat;
  Workbook workBook;
  Sheet sheet;
  int rows = 0;

  try {
 
            workBook = Workbook.getWorkbook(new File("E:/浙江服装9000.xls"));
   sheet = workBook.getSheet(0);
   rows = sheet.getRows();

   String sql = "insert into exceltest(cName,hear,phoneNum,tel,fax,address) values(?,?,?,?,?,?)";
   
         System.out.println(sheet.getCell(0, 1).getContents());
    System.out.println(sheet.getCell(1, 1).getContents());
    System.out.println(sheet.getCell(2, 1).getContents());
    System.out.println(sheet.getCell(3, 1).getContents());
    System.out.println(sheet.getCell(4, 1).getContents());
    System.out.println(sheet.getCell(5, 1).getContents());   
    
   stat = this.getConnection().prepareStatement(sql);
   
  
    
    stat.setString(1,sheet.getCell(0, 1).getContents());
   stat.setString(2,sheet.getCell(1, 1).getContents());
   stat.setString(3,sheet.getCell(2, 1).getContents());
   stat.setString(4,sheet.getCell(3, 1).getContents());
   stat.setString(5,sheet.getCell(4, 1).getContents());
   stat.setString(6,sheet.getCell(5, 1).getContents());   
   
   stat.executeUpdate();
      
 }
   
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ExcelUpload  e = new ExcelUpload (); 
  e.doIt(); 
  System.exit(0);
 }

}

你可能感兴趣的:(j2se)