jdbc操作mysql数据库

 1.新建一个ConnDB.java 文件

 package com.jzm.javabean;
 import java.io.PrintWriter;
import java.sql.*;
public class ConnDB {
 private static String driverName ="com.mysql.jdbc.Driver";
 private static String userName ="root";
 private  static String usrPassword ="6056067";
 private  static String url = "jdbc:mysql://localhost/"+"video"+
          "?user="+userName + "&password="+ usrPassword +
          "&useUnicode=true&characterEncoding=gb2312";
 private static  Connection conn= null;
 private  static Statement sm = null;
 private static PrintWriter out= null;

 public static Connection getConn(){
   try {
            Class.forName(driverName).newInstance();
            conn = DriverManager.getConnection(url);
            sm = conn.createStatement();  
   } catch (Exception e) {
   e.printStackTrace();
         out.print("数据库连接失败");
  }  
   return conn;    
 }
 public  void CloseDb()
 {
  try {
   if(sm != null)
   {
    sm.close();
   }
   conn.close();
  } catch (Exception e) {
    e.printStackTrace();
    out.print("数据库关闭失败");
  }  
 }
}

你可能感兴趣的:(数据库,mysql,exception,jdbc,String,url)