📄 testmysql.java
字号:
/*
If you download mySQL (mostly free, but 30-day shareware
for Microsoft operating systems) from
http://www.mysql.com/
you should run through their tutorial and make sure that you can
start the mysqld server and run the mysql application.
Now download a jdbc driver from
http://www.worldserver.com/mm.mysql/
and make sure that it's on the classpath or its two jar files
are in your "lib/ext" directory. Then you should be able to
use this file. Notice that the sample SQL query, "qStr", does not end
with a semi-colon; with this setup, a closing semi-colon is a
syntax error.
javac TestMySQL.java
java MyNa.utils.TestMySQL
java MyNa.utils.TestMySQL mysql "select user,password from user"
java MyNa.utils.TestMySQL mysql "create table ...."
and so on; we test a mySQL connection to the indicated
database, defaulting to the standard "mysql" database,
and ask a query which defaults to "select * from user".
*/
package MyNa.utils;
import java.sql.*;
public class TestMySQL{
static String driverName="org.gjt.mm.mysql.Driver";
static String dbUrl="jdbc:mysql://localhost:3306/mysql?user=root";
// jdbc:mysql://machineName:portNumber/dbName?option=val
public static void main(String[] args){
try {
Class.forName(driverName).newInstance();
}catch (Exception E) {
System.err.println("Unable to load driver "+driverName);
E.printStackTrace();
}
String dbName=(args.length>0)?args[0]:"mysql";
String qStr=(args.length>1)?args[1]:"SELECT * from user";
dbName="jdbc:mysql://localhost:3306/"+dbName;
try {
System.out.println("got driver "+driverName);
Connection C = DriverManager.getConnection(dbName);
System.out.println("got connection to "+dbName);
DatabaseMetaData dbmd=C.getMetaData();
System.out.println("got connection metadata");
ResultSet RS = dbmd.getTables(null,null,null,null);
String[][]A=MiscDB.resultRowsToStringMatrix(RS);
for(int i=0;i<A.length;i++)
System.out.println(Misc.stringArrayJoin(A[i],",\t"));
Statement S = C.createStatement();
System.out.println("created statement,now try qStr = ["+qStr+"]");
boolean hasRS=S.execute(qStr);
System.out.println("did Query, hasResultSet="+hasRS);
if(hasRS){
RS=S.getResultSet();
A=MiscDB.resultRowsToStringMatrix(RS);
for(int i=0;i<A.length;i++)
System.out.println(Misc.stringArrayJoin(A[i],",\t"));
}else System.out.println("changed "+S.getUpdateCount()+" rows");
S.close();
C.close();
}catch (SQLException E) {
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -