java access sql
JDBC连接MySQL
加载及注册JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
JDBC URL 定义驱动程序与数据源之间的连接
标准语法:
<protocol(主要通讯协议)>:<subprotocol(次要通讯协议,即驱动程序名称)>:<data source identifier(数据源)>
MySQL的JDBC URL格式:
jdbc:mysql//[hostname][:port]/[dbname][?param1=value1][¶m2=value2]….
示例:jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password
常见参数:
user 用户名
password 密码
autoReconnect 联机失败,是否重新联机(true/false)
maxReconnect 尝试重新联机次数
initialTimeout 尝试重新联机间隔
maxRows 传回最大行数
useUnicode 是否使用Unicode字体编码(true/false)
characterEncoding 何种编码(GB2312/UTF-8/…)
relaxAutocommit 是否自动提交(true/false)
capitalizeTypeNames 数据定义的名称以大写表示
建立连接对象
String url="jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password";
Connection con = DriverManager.getConnection(url);
建立SQL陈述式对象(Statement Object)
Statement stmt = con.createStatement();
执行SQL语句
executeQuery()
String query = "select * from test";
ResultSet rs=stmt.executeQuery(query);
结果集ResultSet
while(rs.next())
{rs.getString(1);rs.getInt(2);}
executeUpdate()
String upd="insert into test (id,name) values(1001,xuzhaori)";
int con=stmt.executeUpdate(upd);
execute()
相关文档:
来个简单点的:
1.建个具体的服务实现:
package com.webservice;
@WebService
public class Warehouse {
private Map<String, Double> prices;
public Warehouse() {
prices = new HashMap<String, Double>();
prices.put("Blackwell Toaster", 24.95);
& ......
public class JavaPlus {
public static void main(String[] args) {
int x = 5;
x++;// x = x + 1;//后加加
System.out.println(x);
x--;// x = x - 1;//后减减
System.out.println(x);
++x;// x = x + 1;//前加加
Sys ......
Java 反射是Java语言的一个很重要的特征,它使得Java具体了“动态性”。
在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法?答案是肯定的。这种动态获取类的信 ......
void ModifyDBCode()
{
CString strPath;
::GetModuleFileName(GetModuleHandle(NULL),strPath.GetBuffer(256),256);
strPath.ReleaseBuffer();
int flag=strPath.ReverseFind('\\');
int size=strPath.GetLength();
strPath.Delete(flag,size-flag);
strPath= strPath+ ......