mysql创建 存储过程 并通过java程序调用该存储过程_MySQL
create table users_ning(id primary key auto_increment,pwd int); insert into users_ning values(id,1234);insert into users_ning values(id,12345); insert into users_ning values(id,12); insert into users_ning values(id,123);CREATEPROCEDURE login_ning(IN p_id int,IN p_pwd int,OUT flag int)BEGINDECLARE v_pwd int;select pwd INTO v_pwd from users_ningwhere id = p_id; if v_pwd = p_pwd thenset flag:=1;else select v_pwd;set flag := 0;end if;END package demo20130528;import java.sql.*;import demo20130526.DBUtils;/** * 测试JDBC API调用过程 * @author tarena * */public class ProcedureDemo2 {/** * @param args * @throws Exception*/public static void main(String[] args) throws Exception {System.out.println(login(123, 1234));}/** * 调用过程,实现登录功能 * @param id 考生id * @param pwd 考试密码 * @return if成功:1; if密码错:0; if没有用户:-1 * @throws Exception*/public static int login(int id, int pwd) throws Exception{int flag = -1;String sql = "{call login_ning(?,?,?)}";//*****Connection conn = DBUtils.getConnMySQL();CallableStatement stmt = null;try{stmt = conn.prepareCall(sql);//传递输入参数stmt.setInt(1, id);stmt.setInt(2, pwd);//注册输出参数,第三个占位符的数据类型是整型stmt.registerOutParameter(3, Types.INTEGER);//*****//执行过程stmt.execute();//获得过程执行后的输出参数flag = stmt.getInt(3);//*****}catch(Exception e){e.printStackTrace();}finally{stmt.close();DBUtils.dbClose();}return flag;}}
package demo20130526;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class DBUtils {<span style="white-space:pre"> </span>static Connection conn = null;<span style="white-space:pre"> </span>static PreparedStatement stmt = null;<span style="white-space:pre"> </span>static ResultSet rs = null;<span style="white-space:pre"> </span>static Statement st = null;<span style="white-space:pre"> </span>static String username = null;<span style="white-space:pre"> </span>static String password = null;<span style="white-space:pre"> </span>static String url = null;<span style="white-space:pre"> </span>static String driverName = null;<span style="white-space:pre"> </span>public static Connection getConnMySQL() throws Exception {// 连接mysql 返回conn<span style="white-space:pre"> </span>getUrlUserNamePassWordClassNameMySQL();<span style="white-space:pre"> </span>conn = DriverManager.getConnection(url, username, password);<span style="white-space:pre"> </span>// conn.setAutoCommit(false);设置自动提交为false<span style="white-space:pre"> </span>return conn;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static Connection getConnORCALE() throws Exception {// 连接orcale<span style="white-space:pre"> </span>// 返回conn<span style="white-space:pre"> </span>getUrlUserNamePassWordClassNameORCALE();<span style="white-space:pre"> </span>conn = DriverManager.getConnection(url, username, password);<span style="white-space:pre"> </span>// conn.setAutoCommit(false);<span style="white-space:pre"> </span>return conn;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>private static void getUrlUserNamePassWordClassNameORCALE()<span style="white-space:pre"> </span>throws Exception {<span style="white-space:pre"> </span>// 从资源文件 获取 orcale的username password url等信息<span style="white-space:pre"> </span>Properties pro = new Properties();<span style="white-space:pre"> </span>File path = new File("src/all.properties");<span style="white-space:pre"> </span>pro.load(new FileInputStream(path));<span style="white-space:pre"> </span>String paths = pro.getProperty("filepath");<span style="white-space:pre"> </span>File file = new File(paths + "orcale.properties");<span style="white-space:pre"> </span>getFromProperties(file);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getUrlUserNamePassWordClassNameMySQL() throws Exception {<span style="white-space:pre"> </span>// 从资源文件 获取mysql的username password url等信息<span style="white-space:pre"> </span>Properties pro = new Properties();<span style="white-space:pre"> </span>File path = new File("src/all.properties");<span style="white-space:pre"> </span>pro.load(new FileInputStream(path));<span style="white-space:pre"> </span>String paths = pro.getProperty("filepath");<span style="white-space:pre"> </span>File file = new File(paths + "mysql.properties");<span style="white-space:pre"> </span>getFromProperties(file);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getFromProperties(File file) throws IOException,<span style="white-space:pre"> </span>FileNotFoundException, ClassNotFoundException {// 读资源文件的内容<span style="white-space:pre"> </span>Properties pro = new Properties();<span style="white-space:pre"> </span>pro.load(new FileInputStream(file));<span style="white-space:pre"> </span>username = pro.getProperty("username");<span style="white-space:pre"> </span>password = pro.getProperty("password");<span style="white-space:pre"> </span>url = pro.getProperty("url");<span style="white-space:pre"> </span>driverName = pro.getProperty("driverName");<span style="white-space:pre"> </span>Class.forName(driverName);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void dbClose() throws Exception {// 关闭所有<span style="white-space:pre"> </span>if (rs != null)<span style="white-space:pre"> </span>rs.close();<span style="white-space:pre"> </span>if (st != null)<span style="white-space:pre"> </span>st.close();<span style="white-space:pre"> </span>if (stmt != null)<span style="white-space:pre"> </span>stmt.close();<span style="white-space:pre"> </span>if (conn != null)<span style="white-space:pre"> </span>conn.close();<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static ResultSet getById(String tableName, int id) throws Exception {// 用id来查询结果<span style="white-space:pre"> </span>st = conn.createStatement();<span style="white-space:pre"> </span>rs = st.executeQuery("select * from " + tableName + "where id=" + id<span style="white-space:pre"> </span>+ " ");<span style="white-space:pre"> </span>return rs;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static ResultSet getByAll(String sql, Object... obj)<span style="white-space:pre"> </span>throws Exception {// 用关键字 实现查询 关键字额可以任意<span style="white-space:pre"> </span>sql = sql.replaceAll(";", "");<span style="white-space:pre"> </span>sql = sql.trim();<span style="white-space:pre"> </span>stmt = conn.prepareStatement(sql);<span style="white-space:pre"> </span>String[] strs = sql.split("//?");// 将sql 以? 非开<span style="white-space:pre"> </span>int num = strs.length;// 得到?的个数<span style="white-space:pre"> </span>int size = obj.length;<span style="white-space:pre"> </span>for (int i = 1; i stmt.setObject(i, obj[i - 1]);// 数组下标从0开始<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>if (size for (int k = size + 1; k stmt.setObject(k, null);// 数组下标从0开始<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>rs = stmt.executeQuery();<span style="white-space:pre"> </span>return rs;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void doInsert(String sql) throws SQLException {// 传入 sql 语句<span style="white-space:pre"> </span>// 实现插入操作<span style="white-space:pre"> </span>st = conn.createStatement();<span style="white-space:pre"> </span>st.execute(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void doInsert(String sql, Object... args) throws Exception {// 传入参数<span style="white-space:pre"> </span>// 利用<span style="white-space:pre"> </span>// PreparedStatement<span style="white-space:pre"> </span>// 实现插入<span style="white-space:pre"> </span>// 传入的参数是任意多个 因为有Object 。。。args<span style="white-space:pre"> </span>int size = args.length;// 获得 Object ...obj 传过来的参数的个数<span style="white-space:pre"> </span>stmt = conn.prepareStatement(sql);<span style="white-space:pre"> </span>for (int i = 1; i stmt.setObject(i, args[i - 1]);// 数组下标从0开始<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>stmt.execute();<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static int doUpdate(String sql) throws Exception {// 传入 sql 实现更新操作<span style="white-space:pre"> </span>st = conn.createStatement();<span style="white-space:pre"> </span>int num = st.executeUpdate(sql);<span style="white-space:pre"> </span>return num;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void doUpdate(String sql, Object... obj) throws Exception {<span style="white-space:pre"> </span>// 传入参数 利用 PreparedStatement实现更新<span style="white-space:pre"> </span>// 传入的参数是任意多个 因为有Object 。。。args<span style="white-space:pre"> </span>int size = obj.length;// 获得 Object ...obj 传过来的参数的个数<span style="white-space:pre"> </span>stmt = conn.prepareStatement(sql);<span style="white-space:pre"> </span>for (int i = 1; i stmt.setObject(i, obj[i - 1]);// 数组下标从0开始<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>stmt.executeUpdate(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static boolean doDeleteById(String tableName, int id)<span style="white-space:pre"> </span>throws SQLException {// 删除记录 by id<span style="white-space:pre"> </span>st = conn.createStatement();<span style="white-space:pre"> </span>boolean b = st.execute("delete from " + tableName + " where id=" + id<span style="white-space:pre"> </span>+ "");<span style="white-space:pre"> </span>return b;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static boolean doDeleteByAll(String sql, Object... args)<span style="white-space:pre"> </span>throws SQLException {// 删除记录 可以按任何关键字<span style="white-space:pre"> </span>sql = sql.replaceAll(";", "");<span style="white-space:pre"> </span>sql = sql.trim();<span style="white-space:pre"> </span>stmt = conn.prepareStatement(sql);<span style="white-space:pre"> </span>String[] strs = sql.split("//?");// 将sql 以? 非开<span style="white-space:pre"> </span>int num = strs.length;// 得到?的个数<span style="white-space:pre"> </span>int size = args.length;<span style="white-space:pre"> </span>for (int i = 1; i stmt.setObject(i, args[i - 1]);// 数组下标从0开始<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>if (size for (int k = size + 1; k stmt.setObject(k, null);// 数组下标从0开始<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>boolean b = stmt.execute();<span style="white-space:pre"> </span>return b;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getMetaDate() throws Exception {// 获取数据库元素数据<span style="white-space:pre"> </span>conn = DBUtils.getConnORCALE();<span style="white-space:pre"> </span>DatabaseMetaData dmd = conn.getMetaData();<span style="white-space:pre"> </span>System.out.println(dmd.getDatabaseMajorVersion());<span style="white-space:pre"> </span>System.out.println(dmd.getDatabaseProductName());<span style="white-space:pre"> </span>System.out.println(dmd.getDatabaseProductVersion());<span style="white-space:pre"> </span>System.out.println(dmd.getDatabaseMinorVersion());<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static String[] getColumnNamesFromMySQL(String sql) throws Exception {<span style="white-space:pre"> </span>conn = DBUtils.getConnMySQL();<span style="white-space:pre"> </span>return getColumnName(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static String[] getColumnNamesFromOrcale(String sql)<span style="white-space:pre"> </span>throws Exception {<span style="white-space:pre"> </span>conn = DBUtils.getConnORCALE();<span style="white-space:pre"> </span>return getColumnName(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>private static String[] getColumnName(String sql) throws Exception {// 返回表中所有的列名<span style="white-space:pre"> </span>conn = DBUtils.getConnORCALE();<span style="white-space:pre"> </span>st = conn.createStatement();<span style="white-space:pre"> </span>rs = st.executeQuery(sql);<span style="white-space:pre"> </span>ResultSetMetaData rsmd = rs.getMetaData();<span style="white-space:pre"> </span>int num = rsmd.getColumnCount();<span style="white-space:pre"> </span>System.out.println("ColumnCount=" + num);<span style="white-space:pre"> </span>String[] strs = new String[num];<span style="white-space:pre"> </span>// 显示列名<span style="white-space:pre"> </span>for (int i = 1; i String str = rsmd.getColumnName(i);<span style="white-space:pre"> </span>strs[i - 1] = str;<span style="white-space:pre"> </span>System.out.print(str + "/t");<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>return strs;<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getColumnDataFromMySQL(String sql) throws Exception {// 输出表中的数据<span style="white-space:pre"> </span>conn = DBUtils.getConnMySQL();<span style="white-space:pre"> </span>getColumnData(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getColumnDataFromORCALEL(String sql) throws Exception {// 输出表中的数据<span style="white-space:pre"> </span>conn = DBUtils.getConnORCALE();<span style="white-space:pre"> </span>getColumnData(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getColumnData(String sql) throws Exception {// 输出表中的数据<span style="white-space:pre"> </span>st = conn.createStatement();<span style="white-space:pre"> </span>rs = st.executeQuery(sql);<span style="white-space:pre"> </span>ResultSetMetaData rsmd = rs.getMetaData();<span style="white-space:pre"> </span>System.out<span style="white-space:pre"> </span>.println("/n------------------------------------------------------------------------------------------------------------------------");<span style="white-space:pre"> </span>while (rs.next()) {<span style="white-space:pre"> </span>for (int i = 1; i System.out.print(rs.getString(i) + "/t");<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>System.out.println();<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>System.out<span style="white-space:pre"> </span>.println("------------------------------------------------------------------------------------------------------------------------");<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getTableDataFromOrcale(String sql) throws Exception {// 输出表的列名<span style="white-space:pre"> </span>// 和表中的全部数据<span style="white-space:pre"> </span>conn = DBUtils.getConnORCALE();<span style="white-space:pre"> </span>getTableData(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>public static void getTableDataFromMysql(String sql) throws Exception {// 输出表的列名<span style="white-space:pre"> </span>// 和表中的全部数据<span style="white-space:pre"> </span>conn = DBUtils.getConnMySQL();<span style="white-space:pre"> </span>getTableData(sql);<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>private static void getTableData(String sql) throws SQLException {<span style="white-space:pre"> </span>// getTableDataFromMysql<span style="white-space:pre"> </span>// getTableDataFromOrcale<span style="white-space:pre"> </span>st = conn.createStatement();<span style="white-space:pre"> </span>rs = st.executeQuery(sql);<span style="white-space:pre"> </span>ResultSetMetaData rsmd = rs.getMetaData();<span style="white-space:pre"> </span>int num = rsmd.getColumnCount();<span style="white-space:pre"> </span>System.out.println("ColumnCount=" + num);<span style="white-space:pre"> </span>String[] strs = new String[num];<span style="white-space:pre"> </span>// 显示列名<span style="white-space:pre"> </span>for (int i = 1; i String str = rsmd.getColumnName(i);<span style="white-space:pre"> </span>strs[i - 1] = str;<span style="white-space:pre"> </span>System.out.print(str + "/t");<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>System.out<span style="white-space:pre"> </span>.println("/n------------------------------------------------------------------------------------------------------------------------");<span style="white-space:pre"> </span>while (rs.next()) {<span style="white-space:pre"> </span>for (int i = 1; i System.out.print(rs.getString(i) + "/t");<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>System.out.println();<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>System.out<span style="white-space:pre"> </span>.println("------------------------------------------------------------------------------------------------------------------------");<span style="white-space:pre"> </span>}}

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











이 기사는 MySQL의 Alter Table 문을 사용하여 열 추가/드롭 테이블/열 변경 및 열 데이터 유형 변경을 포함하여 테이블을 수정하는 것에 대해 설명합니다.

InnoDB의 전체 텍스트 검색 기능은 매우 강력하여 데이터베이스 쿼리 효율성과 대량의 텍스트 데이터를 처리 할 수있는 능력을 크게 향상시킬 수 있습니다. 1) InnoDB는 기본 및 고급 검색 쿼리를 지원하는 역 색인화를 통해 전체 텍스트 검색을 구현합니다. 2) 매치 및 키워드를 사용하여 검색, 부울 모드 및 문구 검색을 지원합니다. 3) 최적화 방법에는 워드 세분화 기술 사용, 인덱스의 주기적 재건 및 캐시 크기 조정, 성능과 정확도를 향상시키는 것이 포함됩니다.

기사는 인증서 생성 및 확인을 포함하여 MySQL에 대한 SSL/TLS 암호화 구성에 대해 설명합니다. 주요 문제는 자체 서명 인증서의 보안 영향을 사용하는 것입니다. [문자 수 : 159]

기사는 MySQL Workbench 및 Phpmyadmin과 같은 인기있는 MySQL GUI 도구에 대해 논의하여 초보자 및 고급 사용자를위한 기능과 적합성을 비교합니다. [159 자].

기사는 MySQL에서 파티셔닝, 샤딩, 인덱싱 및 쿼리 최적화를 포함하여 대규모 데이터 세트를 처리하기위한 전략에 대해 설명합니다.

이 기사에서는 Drop Table 문을 사용하여 MySQL에서 테이블을 떨어 뜨리는 것에 대해 설명하여 예방 조치와 위험을 강조합니다. 백업 없이는 행동이 돌이킬 수 없으며 복구 방법 및 잠재적 생산 환경 위험을 상세하게합니다.

MySQL은 B-Tree, Hash, Full-Text 및 Spatial의 4 가지 인덱스 유형을 지원합니다. 1.B- 트리 색인은 동일한 값 검색, 범위 쿼리 및 정렬에 적합합니다. 2. 해시 인덱스는 동일한 값 검색에 적합하지만 범위 쿼리 및 정렬을 지원하지 않습니다. 3. 전체 텍스트 색인은 전체 텍스트 검색에 사용되며 다량의 텍스트 데이터를 처리하는 데 적합합니다. 4. 공간 지수는 지리 공간 데이터 쿼리에 사용되며 GIS 응용 프로그램에 적합합니다.

이 기사에서는 PostgreSQL, MySQL 및 MongoDB와 같은 다양한 데이터베이스에서 JSON 열에서 인덱스를 작성하여 쿼리 성능을 향상시킵니다. 특정 JSON 경로를 인덱싱하는 구문 및 이점을 설명하고 지원되는 데이터베이스 시스템을 나열합니다.
