十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
import java.sql.*;
专注于为中小企业提供成都做网站、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业昌邑免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
public class Test
{
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
//一开始必须填一个已经存在的数据库
String url = "jdbc:mysql://localhost:3306/test?useUnicode=truecharacterEncoding=utf-8";
Connection conn = DriverManager.getConnection(url, "root", "123456");
Statement stat = conn.createStatement();
//创建数据库hello
stat.executeUpdate("create database hello");
//打开创建的数据库
stat.close();
conn.close();
url = "jdbc:mysql://localhost:3306/hello?useUnicode=truecharacterEncoding=utf-8";
conn = DriverManager.getConnection(url, "root", "123456");
stat = conn.createStatement();
//创建表test
stat.executeUpdate("create table test(id int, name varchar(80))");
//添加数据
stat.executeUpdate("insert into test values(1, '张三')");
stat.executeUpdate("insert into test values(2, '李四')");
//查询数据
ResultSet result = stat.executeQuery("select * from test");
while (result.next())
{
System.out.println(result.getInt("id") + " " + result.getString("name"));
}
//关闭数据库
result.close();
stat.close();
conn.close();
}
}
import java.util.Iterator;
public class MyArrayList {
Object[] arr;
int size=0;
public MyArrayList(){
this(10);
}
public MyArrayList(int R){
arr=new Object[R];
}
public int size() {
return size;
}
public void add(Object obj){
if(arr.length=size+1){
arr[size]=obj;
size++;
}else{
int i=arr.length*3/2+1;
Object[] a2=new Object[i];
System.arraycopy(arr,0,a2,0, size);
a2[size]=obj;
size++;
arr=a2;
}
// if(arr.lengthsize+1){
// Object[] a=new Object[size*3/2+1];
// System.arraycopy(arr, 0, a, 0, size);
// arr=a;
// }
// arr[size]=obj;
// size++;
//}
}
public Object get(int i){
if(i0||i=size){
throw new ArrayIndexOutOfBoundsException(i);
}else{
return arr[i];
}
}
public Object set(int i,Object obj){
if(i0||i=size){
throw new ArrayIndexOutOfBoundsException(i);
}else{
size++;
Object o=arr[i];
for(int j=size-1;ji;j--){
arr[j]=arr[j-1];
}
arr[i]=obj;
return o;
}
}
public Object remove(int i){
if(i0||i=size){
throw new ArrayIndexOutOfBoundsException(i);
}else{
Object o=arr[i];
for(int j=i;jsize-1;j++){
arr[j]=arr[j+1];
}
size--;
// System.arraycopy(arr, i+1, arr, i,size-(i+1));
// arr[--size]=null;
return o;
}
}
public Iterator iterator(){
return new MyIterator();//创建迭代器并以父接口类型返回
}
private class MyIterator implements Iterator{//在内部类中可以不
int index=0;
public boolean hasNext(){
return indexsize;
}
public Object next(){
return arr[index++];
}
public void remove(){
MyArrayList.this.remove(index);//内部类调用外部类的方法
}
}
}
//注释为可替换代码
java表格就是java swing。
//创建表头
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
//创建显示数据
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
/*
* JTable还提供了一个重载的构造方法,传入两个Vector
* JTable(Vector rowData, Vector columnNames)
*
*/
final JTable table = new JTable(data, columnNames);
table.setBackground(Color.YELLOW);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JListDemo extends JFrame {
private JPanel topPanel;
private JList listbox;
public JListDemo(){
setTitle( "Simple ListBox Application" );
setSize( 300, 100 );
setBackground( Color.gray );
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
String listData[] =
{
"Item 1",
"Item 2",
"Item 3",
"Item 4"
};
listbox = new JList( listData );
topPanel.add( listbox, BorderLayout.CENTER );
}
public static void main( String args[] ) {
JListDemo mainFrame = new JListDemo();
mainFrame.setVisible( true );
}
}