try{Connection con;\x0d\x0a Statement stmt;\x0d\x0a ResultSet rs;\x0d\x0a int temp;\x0d\x0a Class.forName("com.mysql.jdbc.Driver");\x0d\x0a con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","");//以上是数据库连接,不同的数据管理器有 //不同的驱动和链接方式,以上是mysql的连接\x0d\x0astmt=con.createStatement();\x0d\x0a rs=stmt.executeQuery("select * from student");//执行查询语句,结果赋值给结果集rs\x0d\x0a //结果集是结果于字段编号的映射,每一个字\x0d\x0a //段都有一个编号,最小为1,也就是第一个字段 \x0d\x0a while(rs.next()){\x0d\x0a String names=rs.getString("name");//查询结果转换成字符串。\x0d\x0a \x0d\x0a System.out.println(names);\x0d\x0a\x0d\x0a}rs.close();\x0d\x0a }catch(Exception e){\x0d\x0a e.printStackTrace();\x0d\x0a }
成都创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、内江服务器托管、等保安全、私有云建设等企业级互联网基础服务,欢迎联系:13518219792
try{Connection con;
Statement stmt;
ResultSet rs;
int temp;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","");//以上是数据库连接,不同的数据管理器有 //不同的驱动和链接方式,以上是mysql的连接
stmt=con.createStatement();
rs=stmt.executeQuery("select * from student");//执行查询语句,结果赋值给结果集rs
//结果集是结果于字段编号的映射,每一个字
//段都有一个编号,最小为1,也就是第一个字段
while(rs.next()){
String names=rs.getString("name");//查询结果转换成字符串。
System.out.println(names);
}rs.close();
}catch(Exception e){
e.printStackTrace();
}
首先创建一个连接工厂import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class ConnectionFactory {
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
public ConnectionFactory() {
super();
// TODO Auto-generated constructor stub
} public void OpenConn() throws Exception{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://127.0.0.1:3306/guestbook";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
}catch(Exception e){
System.out.println("创建链接抛出异常为:"+e.getMessage());
}
} public ResultSet executeQuery(String sql) throws Exception{
try{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}catch(Exception e){
System.out.println("执行查询抛出的异常为:"+e.getMessage());
}
return rs;
} public void close() throws Exception{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println("关闭对象抛出的异常:"+e.getMessage());
}
} }
测试类 import java.sql.ResultSet;public class TestJDBC {
public static void main(String[] args) {
ConnectionFactory c= new ConnectionFactory();
try {
c.OpenConn();
String sql="select * from tb_guestbook";
ResultSet rs=c.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(2));
}
c.close();
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTestHarnessV5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.printf("输入你要查找的文字: ");
Pattern pattern = Pattern.compile(scanner.nextLine());
System.out.printf("输入字符串: ");
Matcher matcher = pattern.matcher(scanner.nextLine());
boolean found = false;
while (matcher.find()) {
System.out.printf("找到 \"%s\" 开始下标 %d 结束下标 %d.%n",
matcher.group(), matcher.start(), matcher.end());
found = true;
}
if (!found) {
System.out.printf("没有找到.%n");
}
}
}
}