jdbc连接数据库的3种语句

  3种jdbc连接分别是:普通的jdbc,PreparedStatement,CallableStatement

  

package jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
import static space.tools.print.Print.printfgf;

public class Test {
 private Connection conn = null;
 private ResultSet rs = null;
 protected Logger log = Logger.getLogger(this.getClass());

 public Test() {
  initConn("jdbc:mysql://localhost:3306/zhaopin", "root", "your_pswd");
 }

 public Test(String url, String username, String pswd) {
  initConn(url, username, pswd);
 }

 public static void main(String[] args) {
  Test test = new Test();
  test.jdbc();
  printfgf();
  test.jdbcPS();
  printfgf();
  test.jdbcCS();
 }

 /**
  * 普通的jdbc语句
  */
 public void jdbc() {
  try {
   Statement stmt = conn.createStatement();
   String sql = "select * from article limit 1 ;";
   rs = stmt.executeQuery(sql);
   while (rs.next()) {
    System.out.println(rs.getString(1) + "," + rs.getString(2) + "," + rs.getString(3) + ","
      + rs.getString(4));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

 /**
  * 使用 PreparedStatement 语句
  */
 public void jdbcPS() {
  try {
   String sql = "select * from article limit ? ";
   PreparedStatement ps = conn.prepareStatement(sql); // 以 preparedstatement sql 语句 字符串作为参数
   ps.setInt(1, 2); // 填充 ? 参数
   rs = ps.executeQuery(); // 执行 preparedstatement 语句,获得结果;
   while (rs.next()) {
    System.out.println(rs.getString(1) + "," + rs.getString(2) + "," + rs.getString(3) + ","
      + rs.getString(4));
   }

  } catch (SQLException e) {
   e.printStackTrace();
  }

 }

 /**
  * 使用 CallableStatement 语句,调用数据库中的存储过程
  */
 public void jdbcCS() {
  // 存储过程: CREATE PROCEDURE listArticle () SELECT * FROM article LIMIT 3
  try {
   CallableStatement cs = conn.prepareCall("{call listArticle()}");
   rs = cs.executeQuery();
   while (rs.next()) {
    System.out.println(rs.getString(1) + "," + rs.getString(2) + "," + rs.getString(3) + ","
      + rs.getString(4));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }

 }

 private void initConn(String url, String username, String pswd) {
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(url, username, pswd);
   System.out.println("连接成功");
  } catch (SQLException ex) {
   System.out.println(ex.getMessage() + "数据库配置错误");
  } catch (ClassNotFoundException ex) {
   System.out.println(ex.getMessage() + "驱动加载错误");
  }
 }
}

 注:space.tools.print.Print 类,是自定义的一个打印输出的工具类,

 

package space.tools.print;

/**
 * This class comes from System.out.println() and System.out.print()
 * Helps to use less words to print something.
 * Before using the static methods in this class,use "import space.tools.print;" to import this class
 * After import this class,you can use the static methods without "Print." before,just white the method'name is ok.
 */
public class Print
{
	/**
	 * print something,then goto a new line.
	 */
	public static void print(Object o)
	{
		System.out.println(o);
	}
	/**
	 * print nothing,just goto a new line.
	 */
	public static void print()
	{
		System.out.println();
	}
	/**
	 * just print something in present line.
	 */
	public static void printnb(Object o)
	{
		System.out.print(o);
	}
	
	/**
	 * print fgf.
	 */
	public static void printfgf()
	{
		System.out.println("\n-----------------------");
	}
	public static void printfgf(int n)
	{
		char c='-';
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<n;i++)
			sb.append(c);
		System.out.println(sb);
	}
	public static void printfgf(char c,int n)
	{
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<n;i++)
			sb.append(c);
		System.out.println(sb);
	}
	public static void printArray(Object[] os)
	{
		StringBuilder sb=new StringBuilder("["+(os.length==0?"":"\n"));
		for(Object o:os)
			sb.append(o+"\n");
		sb.append("]");
		System.out.println(sb);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值