myeclipse jdbc连接mysql数据库

package zms.roya.comm;

import java.sql.*;
import java.sql.SQLException;
import java.lang.Math;


public class jdbcmsyql5 {

 // String createtable="create table user( 'id' int AUTO_INCREMENT UNIQUE
 // ,'name' varchar(30) , 'age' int )";
 // private String jdbcstr = "com.mysql.jdbc.Driver"; 驱动名称mysql-connector-java-5.0.7-bin.jar
 private String jdbcurl = "jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GBK";
 private String dbusername = "root";
 private String dbpwd = "royasoft";

 public static void main(String[] args) {
  
  int x=(int)(Math.random()*10000);//获取 0-1000的随即数字
        String instr = "INSERT INTO usertest(NAME,age) VALUES ('zms"+x+"',"+x+");"; //为了好看 插入不同的数据
       
  String qsql = "select * from usertest;";

  jdbcmsyql5 xy = new jdbcmsyql5();
  if (xy.addred(instr)) {
   System.out.println("插入成功");
  } else
   System.out.println("插入失败");

  if (xy.queryred(qsql)) {
   System.out.println("查询成功");
  } else
   System.out.println("查询失败");

 }

 /**
  * @获取 连接
  */
 public Connection dbconnet()

 {
  Connection connx;

  try {
   // 加载驱动

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

   // 连接数据库

   System.out.println("获取到得连接URL是" + jdbcurl);
   connx = java.sql.DriverManager.getConnection(jdbcurl, dbusername,
     dbpwd);
   return connx;
  } catch (ClassNotFoundException e) {
   System.out.println("驱动包有问题");
   e.printStackTrace();

  }

  catch (SQLException e) {
   System.out.println("连不上,可能数据库端IP和端口原因,也可能是账号密码原因,请看报错信息");
   e.printStackTrace();

  }
  return null;

 }

 /*
  * public Connection getcon() { Connection con; try { //加载驱动程序
  * Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //另外一个常用的驱动 mm.mysql-2.0.4-bin.jar
  * con=DriverManager.getConnection(url,user,pwd); return con; }
  *
  * catch(ClassNotFoundException e) { System.out.println("加载驱动程序出错"); }
  *
  *
  * catch(Exception e) { System.out.println("出现了好大的错误"); }
  *
  * return null; }
  */
 public boolean addred(String insertsql) {
  Connection conn;
  java.sql.Statement stmt = null;

  try {
   System.out.println("jdbcurl" + jdbcurl + dbusername + dbpwd);
   conn = dbconnet();
   stmt = conn.createStatement();
   String sqlstr = insertsql;
   System.out.println("插入语句" + sqlstr);
   stmt.executeUpdate(sqlstr);

   stmt.close();
   conn.close();
   return true;
  } catch (SQLException e) {
   e.printStackTrace();
   return false;
  }

 }

 public boolean queryred(String qsql) {
  Connection conn;
  java.sql.Statement stmt = null;
  java.sql.ResultSet rs = null;

  try {
   System.out.println("jdbcurl" + jdbcurl + dbusername + dbpwd);
   conn = dbconnet();
   stmt = conn.createStatement();
   System.out.println("获取到的查询语句" + qsql);
   rs = stmt.executeQuery(qsql);
   while (rs.next()) {
    System.out.print("编号" + rs.getInt(1));
    System.out.print("名字" + rs.getString("name"));
    System.out.println("年龄" + rs.getString("age"));

   }
   rs.close();
   stmt.close();
   conn.close();
   return true;
  } catch (SQLException e) {
   e.printStackTrace();
   return false;
  }
 }
}

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